plotting a struct in a loop

8 views (last 30 days)
Benjamin on 9 Nov 2018
Edited: madhan ravi on 12 Nov 2018
I have this code:
I also want to plot S.A601 up to S.A620
This does not work:
for i = 1:1:20
hold on
How can I loop through each one or do I have to manually type each plot command ?

Accepted Answer

Image Analyst
Image Analyst on 9 Nov 2018
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
grid on;
  1 Comment
Benjamin on 12 Nov 2018
Edited: madhan ravi on 12 Nov 2018
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?

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!