plotting a struct in a loop

4 Ansichten (letzte 30 Tage)
Benjamin
Benjamin am 9 Nov. 2018
Bearbeitet: madhan ravi am 12 Nov. 2018
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 ?

Akzeptierte Antwort

Image Analyst
Image Analyst am 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
drawnow;
end
grid on;
  1 Kommentar
Benjamin
Benjamin am 12 Nov. 2018
Bearbeitet: madhan ravi am 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? https://www.mathworks.com/matlabcentral/answers/429433-loading-in-excel-data-with-loop-and-plotting

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by