plotting a struct in a loop

조회 수: 3 (최근 30일)
Benjamin
Benjamin 2018년 11월 9일
편집: madhan ravi 2018년 11월 12일
I have this code:
semilogy(S.A600(:,1),S.A600(:,2));
I also want to plot S.A601 up to S.A620
This does not work:
for i = 1:1:20
semilogy(S.A60(i)(:,1),S.A60(i)(:,2));
hold on
end
How can I loop through each one or do I have to manually type each plot command ?

채택된 답변

Image Analyst
Image Analyst 2018년 11월 9일
Try this:
% S.A601 = rand(10, 2); % Create sample data.
% S.A602 = rand(10, 2);
% S.A603 = rand(10, 2);
% S.A604 = rand(10, 2);
% S.A605 = rand(10, 2);
% S.A606 = rand(10, 2);
fn = fieldnames(S)
for k = 1:length(fn)
thisS = S.(fn{k})
x = thisS(:, 1);
y = thisS(:, 2);
fprintf('Printing field #%d.\n', k);
semilogy(x, y);
hold on
drawnow;
end
grid on;
  댓글 수: 1
Benjamin
Benjamin 2018년 11월 12일
편집: madhan ravi 2018년 11월 12일
your answer worked great! now that I have tried loading the data in with a loop, it does not seem to work. Could you check here and maybe see where my mistake is? https://www.mathworks.com/matlabcentral/answers/429433-loading-in-excel-data-with-loop-and-plotting

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by