Modify a plot in a for loop

4 visualizaciones (últimos 30 días)
Jorge Navarro Barrios
Jorge Navarro Barrios el 31 de Mzo. de 2020
Editada: Adam Danz el 1 de Abr. de 2020
I would like to know if it is possible to modifidy all the curve generated by this for loop. For example to add a distintive marker for each one of the curves.
omega = 0:0.01:3;
for zeta = [0 0.15 0.5 1 2]
mu = 1./(sqrt((1-omega.^2).^2+4*zeta^2*omega.^2));
p = plot(omega,mu);
hold on
end
p.Marker = '*';
p.MarkerIndices = 1:7:length(omega);
With this code, Matlab only apply the changes to the last curve plotted.

Respuesta aceptada

Adam Danz
Adam Danz el 31 de Mzo. de 2020
Editada: Adam Danz el 31 de Mzo. de 2020
The idea is to collect all of the line object handles.
I've also restructured your code; see the inline comments for details.
omega = 0:0.01:3;
zeta = [0 0.15 0.5 1 2]; % Defined outside of the loop
p = gobjects(5,1); % pre-allocate, one for each zeta
hold on % move this out of the loop
for i = 1:numel(zeta) % Loop over integer values starting with 1
mu = 1./(sqrt((1-omega.^2).^2+4*zeta(i)^2*omega.^2)); % note zeta(i)
p(i) = plot(omega,mu); % store all handles
end
set(p, 'Marker', '*'); % set marker for all handles in p
set(p, 'MarkerIndices', 1:7:length(omega)); % set MarkerIndices for all p
  2 comentarios
Jorge Navarro Barrios
Jorge Navarro Barrios el 1 de Abr. de 2020
Wow! Thanks! That works just fine. And if I just want to asign a different marker to each curve is as simple as:
set(p(1), 'Marker', '*'); %for zeta = 0
set(p(2), 'Marker', 'o'); %for zeta = 0.15
Again! Thank you very much!
Adam Danz
Adam Danz el 1 de Abr. de 2020
Editada: Adam Danz el 1 de Abr. de 2020
Exactly!
And if you want to assign different markers to each object,
set(p, {'Marker'}, {'+','^','s','o','v'}') % Both column vectors

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Specifying Target for Graphics Output 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