plotting a struct in a loop

2 vues (au cours des 30 derniers jours)
Benjamin
Benjamin le 9 Nov 2018
Modifié(e) : madhan ravi le 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 ?

Réponse acceptée

Image Analyst
Image Analyst le 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 commentaire
Benjamin
Benjamin le 12 Nov 2018
Modifié(e) : madhan ravi le 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by