Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument to dynamic structure reference must evaluate to a valid field name

I am getting this error "Argument to dynamic structure reference must evaluate to a valid field name." I have a struct 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 try and 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 at.

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

where metabString is a string of one of the names for example 'PCho0x25SD' spectData has 4 sub structs in total all like this one I displayed. What am I doing wrong?? It is a double so it should be ok I thought.

like image 632
Ben Fossen Avatar asked Dec 13 '22 00:12

Ben Fossen


2 Answers

Matlab can give this sometimes misleading error message when you accidentally pass a cell array 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 (test the actual type of your field name with e.g. whos), it should help to say f = char(f).

like image 198
quazgar Avatar answered Dec 28 '22 06:12

quazgar


A string is represented in matlab as cell. while literal strings are of type char array. They prints differently. A cell string prints as

ans =

    'abc'

while a regular char array prints as

ans = 
abc

Now comes to the difference of the two builtin functions: cellstr converts char array to string while char converts a cell string to char array.

So in your case, you should use char(MetabMapSDString) as your dynamic struct reference.

like image 35
Chivalryman Avatar answered Dec 28 '22 07:12

Chivalryman