plotting a struct in a loop

2 visualizaciones (últimos 30 días)
Benjamin
Benjamin el 9 de Nov. de 2018
Editada: madhan ravi el 12 de Nov. de 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 ?

Respuesta aceptada

Image Analyst
Image Analyst el 9 de Nov. de 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 comentario
Benjamin
Benjamin el 12 de Nov. de 2018
Editada: madhan ravi el 12 de Nov. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by