MATLAB: How to get the values from the structure

arraycodeMATLABstruct

Hey, maybe my question is pretty easy to solve. I have a struct array, build by a "solve" function. I need to get those values, but I don't know how many values the struct could get, because it depends in the input of the user. As you can see, this make it well but only for 2 values, if I get a structure for 3 values even if I change the value of "i" it doesn't work. Thanks in advance for all your help.
pc=solve (y, [x]);
pucr= fieldnames (pc);
for i= 1:2
vals(i, 1)= getfield(pc,pucr{i});
end

Best Answer

  • Use numel() to fine the number of elements in pucr.
    pc=solve (y, [x]);
    pucr= fieldnames (pc);
    for i= 1:numel(pucr)
    vals(i, 1)= getfield(pc,pucr{i});
    end
    Now the for loop will run as many times as the number of fields in pucr.