How to gradually plot two simultaneous graphs in MATLAB gui?

Hello Everyone,
I am trying to plot two graphs in MATLAB gui simultaneously. Both of these should be plotted at the same time. I have tried looping them in in a single loop but it plots them in the same axes, whereas having two loops makes them plot one after other.
Here's my code:
a = 0:0.01:1;
x = sin(2*pi*3*a);
y = cos(2*pi*a);
figure(1)
for i = 1:length(a)
plot(a(1:i),x(1:i))
pause(0.05)
end
figure(2)
for i = 1:length(a)
plot(a(1:i),y(1:i))
pause(0.05)
end
Can anyone help me figure out a way to plot them both simultaneously?

 採用された回答

Walter Roberson
Walter Roberson 2021 年 6 月 13 日

0 投票

a = 0:0.01:1;
x = sin(2*pi*3*a);
y = cos(2*pi*a);
fig1 = figure(1);
ax1 = axes(fig1);
fig2 = figure(2);
ax2 = axes(fig2);
for i = 1:length(a)
plot(ax1, a(1:i),x(1:i))
plot(ax2, a(1:i),y(1:i))
pause(0.05)
end
But like I wrote before, I recommend that you read about animatedline()

3 件のコメント

Saad Rana
Saad Rana 2021 年 6 月 13 日
It works for normal use but in matlab gui, we use
axes(handles.axes1)
can you tell how can I implement this into what you told?
I have tried
a = 0:0.01:1;
x = sin(2*pi*3*a);
y = cos(2*pi*a);
fig1 = handles.axes1;
ax1 = axes(fig1);
fig2 = handles.axes2;
ax2 = axes(fig2);
for i = 1:length(a)
plot(ax1, a(1:i),x(1:i))
plot(ax2, a(1:i),y(1:i))
pause(0.05)
end
but it gives some errors. Any help would be appreciated.
Walter Roberson
Walter Roberson 2021 年 6 月 13 日
a = 0:0.01:1;
x = sin(2*pi*3*a);
y = cos(2*pi*a);
for i = 1:length(a)
plot(handles.axes1, a(1:i),x(1:i))
plot(handles.axes2, a(1:i),y(1:i))
pause(0.05)
end
Saad Rana
Saad Rana 2021 年 6 月 13 日
Thanks a lot, Sir. This was a great help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品

リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by