How can I get the values from my structure?

조회 수: 3 (최근 30일)
Abner Ojeda
Abner Ojeda 2020년 5월 1일
댓글: Ameer Hamza 2020년 5월 1일
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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 1일
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.
  댓글 수: 2
Abner Ojeda
Abner Ojeda 2020년 5월 1일
I've just tried it and it works pretty well. I tried with different numbers of variables (1, 5, 10, 8), in all cases works awesome. Thanks for the help, Ameer :)
Ameer Hamza
Ameer Hamza 2020년 5월 1일
I am glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by