plotting a struct in a loop

11 ビュー (過去 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 ?
  1 件のコメント
Stephen23
Stephen23 2018 年 11 月 10 日

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by