1

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.
m.s.
  • 16,063
  • 7
  • 53
  • 88
dede
  • 1,129
  • 5
  • 15
  • 35
  • what's the output of `class(EEG.event.type)`? – m.s. Oct 29 '15 at 13:35
  • `ans = "..." \n ans = "..." \n ...` 450 times. – Mad Physicist Oct 29 '15 at 13:44
  • If I use class(EEG.event.type), I get the error: Error using class The CLASS function must be called from a class constructor. While if I use class(EEG.event(1).type) , I get the answer char. – dede Oct 29 '15 at 13:45
  • Misread the question. Accessing the field of a struct array like that is not allowed. The structs can have any type stored in the same field. – Mad Physicist Oct 29 '15 at 13:46

2 Answers2

0

One way to get around the fact that struct fields can contain anything at all is to write a for loop to do the replacement for fields that need it. The following is a pretty generic loop that will cover all the fields of the structs, check if they are a string and do the replacement:

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

Note the use of dynamic field names and a precomputed list of fields. You may also find the following question and answer useful: Iterating through struct fieldnames in MATLAB

Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
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
Tony Gweesip
  • 194
  • 1
  • 9
  • Thanks for the answer, it works perfectly! So, in the case that I know in advance the fields to modify, could you please suggest how should I write the command without doing a loop through all those fields? In my case for example, the columns 3 and 4, and 6 to 10 are all columns of the EEG.event that I need to modify. Thank you. – dede Oct 29 '15 at 17:06
  • @dede See edit for additional suggestions. Note that these solutions are very similar to what Mad Physicist proposed in his answer, as there aren't any built-in functions to deal with arrays of structures (as far as I know). I did my best to make sure the code works, but I don't have Matlab on my current computer, so I could not verify that it is error free! – Tony Gweesip Oct 29 '15 at 21:40