使用strrep删除Matlab中的双引号

时间:2015-10-29 13:31:53

标签: matlab str-replace double-quotes

我有一个1x1结构( EEG ),有41个字段。其中一个字段称为事件,是一个1x450结构,具有不同的字段,其中一些是字符串和一些数字值。我想删除字符串字段/列中出现的双引号。例如,我有一个名为 type 的列,其中包含'""'''""''等等。我希望将字符串转换为' acc',' bacc'等。

我试过了:

strrep(EEG.event.type, '"','');

并返回此错误:

Error using strrep
Too many input arguments.

我还尝试直接选择要删除双引号的列:

strrep(EEG.event(:,[3:4 6:10]),'"','');

它给了我这个错误:

Error using strrep
Conversion to double from struct is not possible.

2 个答案:

答案 0 :(得分:0)

解决结构字段可以包含任何内容这一事实的一种方法是编写for循环来替换需要它的字段。以下是一个非常通用的循环,它将覆盖结构的所有字段,检查它们是否为字符串并进行替换:

names = fieldnames(EEG.event)
for e = 1:numel(EEG.event)
    for f = 1:numel(names)
        if ischar(EEG.event(e).(names{f}))
            strrep(EEG.event(e).(names{f}), '"', '')
        end
    end
end

请注意使用动态field names和预先计算的字段列表。您还可以找到以下问题和答案:Iterating through struct fieldnames in MATLAB

答案 1 :(得分:0)

If EEG.event.type is a string for each EEG.event structure in the array (as your output indicates), you can do this:

for i=1:numel(EEG.event)
    EEG.event(i).type = strrep(EEG.event(i).type,'"','')
end

This also assumes that you are only trying to modify the type field and none of the other fields, although it would work on other fields as well if they are the same format. If you know in advance which fields you are trying to modify and that they are the correct type, this saves you from having to loop through all fields in the structure.

Start of Edit

Unfortunately, I don't know of any non-looping methods of acting on multiple fields of an array of structures simultaneously. I have yet to find a built-in function that would allow you to do this, other than structfun(), which only works on scalar structures. Generally I tend to avoid arrays of structures, preferring structures of arrays for this very reason, but based on your question, I'm guessing your data is coming from an external source and you have no control over formatting. You could always do one of the following:

fieldsToModify = {'type','someOtherField','yetAnotherField'};
for i=1:numel(EEG.event)
    for j=1:numel(fieldsToModify)
        EEG.event(i).(fieldsToModify{j}) = strrep(EEG.event(i).(fieldsToModify{j}),'"','');
    end
end

This would save you from having to check all the fields, but doesn't save you from nested for loops. You could also do this:

allFields = fieldnames(EEG.event)
for i=1:numel(EEG.event)
    for j=1:numel(allFields)
        switch(allFields{j})
            case {'type','someOtherField','yetAnotherField'}:
                EEG.event(i).(allFields{j}) = strrep(EEG.event(i).(allFields{j}),'"','');
            %Should you discover other fields that need modification
            case {list of other fields to modify}:
                % Some function to modify them
            otherwise:
                % Default action
         end
     end
 end

Again, not ideal because of nested for loops, but will at least be easier to modify later if you discover there are more fields that need to be modified for your purposes.

Finally, the least elegant of solutions:

for i=1:numel(EEG.event)
    EEG.event(i).type = strrep(EEG.event(i).type,'"','');
    EEG.event(i).field1 = strrep(EEG.event(i).field1,'"','');
    % And so on for all the fields that need to be modified
end