2

I'm trying hard to obtain values from a structure, defining the fields as strings, but I can´t do it.

It always returns the error:

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

The piece of code I'm using looks like:

S = load('ResLongspcSt.mat');

j = fieldnames(S);
x = length(j);

for i = 1:x
    x2(i) = S.([j(i)])(500,i);
end

Thank you very much for your advice!

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • possible duplicate of [How do I access structure fields dynamically?](http://stackoverflow.com/questions/1882035/how-do-i-access-structure-fields-dynamically) and [How to create and access structure which structure corresponds to given variable list in matlab?](http://stackoverflow.com/questions/12213330/how-to-create-and-access-structure-which-structure-corresponds-to-given-variable) – Eitan T Dec 17 '13 at 13:01

1 Answers1

3

Try this... Cell array elements are accessed with curly braces, not with parentheses.

for i = 1:x
    tmp   = s.(j{i});
    x2(i) = tmp(500,i);
end
Buddhima Gamlath
  • 2,318
  • 1
  • 16
  • 26