Search code examples
matlabstr-replacedouble-quotes

Using strrep to remove double quotes in Matlab


I have a 1x1 structure (EEG) with 41 fields. One of these fields is called event and is a 1x450 structure, with different fields, some of which are strings and some numeric values. I would like to remove the double quotes that appear in the strings fields/columns. For example, I have a column called type that has '"acc"', "bacc"', etc. and I would like to transform the strings into 'acc', 'bacc', etc.

I have tried:

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

and it returns this error:

Error using strrep
Too many input arguments.

I also tried to directly select the columns in which I want to remove the double quotes:

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

and it gives me this error:

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

Solution

  • 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