The argument to reference the dynamic structure must be evaluated using a valid field name

I get this error. "The argument to reference the dynamic structure must be evaluated to a valid field name." I have a structure called spectData and it looks like this:

spectData{1} = data: [256x26 double] textdata: {1x26 cell} colheaders: {1x26 cell} Row: [256x1 double] Col: [256x1 double] Cho: [256x1 double] Cho0x25SD: [256x1 double] Cho0x2FCit: [256x1 double] PCho: [256x1 double] PCho0x25SD: [256x1 double] 

I am trying to assign this in a function call, the line of code looks like this. This is the line of code that Matlab says, the error is in.

  SDdata = spectData{sliceNum - firstSlice}.(MetabMapSDString); 

where metString is a string of one of the names, for example, "PCho0x25SD" spectData has 4 substructures in total, all as shown. What am I doing wrong?? It's double, so everything should be fine, I thought.

+4
source share
2 answers

Matlab may give this sometimes misleading error message when you accidentally pass an array of cells instead of a string. The following example gives the same error:

 fields = {'foo', 'bar'} s = struct('foo', 23, 'bar', pi) for f = fields disp(f) s.(f) = 0 end 

If this is your problem (check the actual type of your field name, for example, whos ), this should help say f = char(f) .

+11
source

A string is represented in Matlab as a cell. and literal strings have an array of type char. They are printed in different ways. The cell row is printed as

 ans = 'abc' 

while a regular char array prints like

 ans = abc 

Now the difference arises between two built-in functions: cellstr converts the char array to a string, and char converts the cell string to a char array.

So, in your case, you should use char(MetabMapSDString) as your dynamic link to the structure.

+2
source

All Articles