How to plot a second graph when the first graph still plotting?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joy
el 17 de Mayo de 2017
Editada: Cam Salzberger
el 17 de Mayo de 2017
For example, I have 2 functions. y1 = x; y2 = sin(x);
y1 start to plot when time is 0, I'd like to plot y2 when the time is 10s, and y1&y2 plotting together.
When the time is 50s, script stop running.
How to achieve this? Do I need a timer for that?
0 comentarios
Respuesta aceptada
Cam Salzberger
el 17 de Mayo de 2017
Editada: Cam Salzberger
el 17 de Mayo de 2017
Hello Joy,
It sounds like you want to basically plot in real time, or something similar to that. You could do this in a simple loop, or you could use a timer. There are advantages and disadvantages to both.
Either way you choose, I would highly recommend setting up the figure(s), axes, and plotted lines before you begin either the loop or the timer. This will allow the main body of the loop/timer to focus on just updating the values in the lines, which will make the actual plotting take less time, and save on memory and code complexity.
A loop works if you plan to always have the second line update at a fixed interval or rate relative to the first line. It sounds like your application is pretty simple, where you can just have the lines update at the same time, just one starts later than the other. Something like:
n = 10;
x = 1:n;
y1 = x.^2;
y2 = 5*log(x);
figure
axes
xlim([min(x) max(x)])
ylim([min([y1 y2]) max([y1 y2])])
hold on
h1 = plot(NaN,NaN,'-k');
h2 = plot(NaN,NaN,'-b');
offset2 = 5;
for t = 1:n+offset2
h1.XData = x(1:min(t,n));
h1.YData = y1(1:min(t,n));
h2.XData = x(1:t-offset2);
h2.YData = y2(1:t-offset2);
pause(1)
end
A timer would be very similar. You could just specify the timer callback function to update the plot in the same way. The disadvantage is it's no longer one script - you'd need at least a local function. The advantage is that the code is no longer blocking. So if you wanted to start and run other code while some big plot is generating, that's possible. Another advantage is that, depending on your timer settings, it may be more accurate for plotting closer to "each second" than "pause" could ever get you. Though you could probably combine a pause with a tic/toc to get closer if you wanted to.
Hope this helps!
-Cam
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!