Varios gráficos en 1 ciclo, cada iteración agrega una línea en cada figura
Frecuentes
Visto 11,583 veces
4
Trying to engineer the following in Matlab:
** loop start;
y(:,i) = function of x;
z(:,i) = function of x;
plot(x,y(:,i)) on figure 1, hold all;
plot(x,z(:,i)) on figure 2, hold all;
** loop end;
add title, legend, etc for figure 1 (NB: we have multiple lines);
add title, legend, ets for figure 2 (NB: same, have multiple lines for the legend);`
Tried multiple combinations without much luck. Managed to get 2 figures but only the 2-nd displays multiple lines, not the first. And can't figure how to add legends to these 2 correctly.
2 Respuestas
4
Save a handle to each figure, and to each axis object:
fh1 = figure;
hold all;
ah1 = gca;
fh2 = figure;
hold all;
ah2 = gca;
for i=1:N
y(:,i) = function of x;
z(:,i) = function of x;
plot(ah1, x, y(:,i)); %# tell it which axis to use (ah1)
plot(ah2, x, z(:,i)); %# (ah2)
end
legend(ah1, ...) %# legend options here
legend(ah2, ...) %# and the other legend
%# note: you can set figure properties for each using fh1, fh2 handles.
Respondido 26 ago 12, 23:08
0
Puedes hacerlo:
figHandle1 = figure(1);
figHandle2 = figure(2);
Then when you want to plot on that figure do this:
figure(figHandle1) %Plot on figure 1
%ie plot(someStuff)
figure(figHandle2) %Plot on figure 2
Also its the same for the title and stuff, you jjust need to identify which figure by doing:
figure(handle);
Espero que esto ayude.
Respondido 26 ago 12, 23:08
figure(h)
will bring the handle h
to the front, so new things are put into the figure - so, this will work. However, it is comparatively slower than just specifying which handle to use by giving the handle to the plot command (i.e. what I did in my answer). This can be important if you'd be switching figures often (as the OP would be doing, twice each time in a loop). - tmperace
@tmpearce I didn't know that, but thanks, hopefully the OP isnt running that many loops. - Fantástico señor Fox
I tried this before and tried again just now. The result -- 1 line on figure 1 and 10 lines on figure 2. I am reluctant to put all the code here as functions are very sophisticated. But everything works if I 'kill' figure 2 & ask to plot only figure 1, then all 10 lines are there. - galaxia5727
Actually I just found the bug: there should be "hold all" used twice after every plot inside the loop. - galaxia5727
Works great. If you have a chance to answer -- why is it ok to put "hold all" in front of the loop? I did handles too, apparently 2-nd "hold all" was missing or in a wrong place. - galaxia5727
"hold all" works on the current axes. If an axes object doesn't exist, it creates one. That's why it works before you plot anything. - tmperace
How should one do "hold off"'s? Something like figure(1); hold off; figure(2); hold off; - galaxia5727
Desde el documentación:
hold off
resets hold state to the default behavior, in which MATLAB clears the existing graph and resets axes properties to their defaults before drawing new plots. - tmperaceWell, not like I would want to get rid of all of my lines but theoretically once I have 'hold on' activated on both figures I "need" (or might want) to deactivate it on both too. I don't see how to use 'ah1' and 'ah2' to implement such de-activations => hence the question. - galaxia5727