I am runny a loop over set of function and in the end I want some graphs of different data. there are three variables let say a, b and c.I have generated a graph using:
plot(iteration,a,iteration,b,iteration,c)
plot(a,b)
bt it is giving the 2nd one only. I want another graph of A on x axis and C on y axis likewise C on x axis and b on y axis. I cant figure out how to get multiple plots because it is generating plot of last command only. I tried

 採用された回答

Walter Roberson
Walter Roberson 2018 年 7 月 28 日

5 投票

Do not use figure() as a variable name.
fig1 = figure(1);
ax1 = axes('Parent', fig1);
h = plot(ax1, Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');
legend(h, 'total_util','d_quantity','Price');
fig2 = figure(2);
ax2 = axes('Parent', fig2);
h = plot(ax2, total_util);
The simple version of that is
figure(1);
plot(Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');
legend('total_util','d_quantity','Price');
figure(2);
plot(total_util);
but I recommend using the longer version: with the shorter version, graphics can end up going places you do not want in some cases. See https://www.mathworks.com/matlabcentral/answers/?term=tag%3Aalways-parent

その他の回答 (1 件)

Ben Frankel
Ben Frankel 2018 年 7 月 28 日
編集済み: Ben Frankel 2018 年 7 月 28 日

1 投票

You need to use hold on after the first plot and hold off after the last plot if you want multiple plots in the same figure. Use figure before all of the plots to put them in a new figure.

10 件のコメント

summyia qamar
summyia qamar 2018 年 7 月 28 日
this is not working because both plots are showing on the same graph I want 2 different graph windows
Walter Roberson
Walter Roberson 2018 年 7 月 28 日
  • "hold on" if you want the graphs on the same axes
  • subplot() if you want the graphs on different axes in the same figure
  • figure() if you want to create different windows
summyia qamar
summyia qamar 2018 年 7 月 28 日
with figure(1) it is giving indexing error
Walter Roberson
Walter Roberson 2018 年 7 月 28 日
If figure(1) is giving an indexing error, then you somehow have accidentally created an empty variable named figure
which figure
summyia qamar
summyia qamar 2018 年 7 月 28 日
I have written this after the loop end.. and no other FIGURE word used
figure(1)=plot(Iteration,total_util*100,'b',Iteration,DD,'r',Iteration,PP,'g');
legend('total_util','d_quantity','Price')
figure(2)=plot(total_util);
summyia qamar
summyia qamar 2018 年 7 月 28 日
Error
Unable to perform assignment
because the left and right sides
have a different number of
elements.
figure(1)=plot(Iteration,total_util*100,'b',Iteration,DD,'r',Iteration,PP,'g');
Walter Roberson
Walter Roberson 2018 年 7 月 28 日
Do not use figure() as a variable name.
fig1 = figure(1);
ax1 = axes('Parent', fig1);
h = plot(ax1, Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');
legend(h, 'total_util','d_quantity','Price');
fig2 = figure(2);
ax2 = axes('Parent', fig2);
h = plot(ax2, total_util);
summyia qamar
summyia qamar 2018 年 7 月 28 日
yes it worked. thankyou
summyia qamar
summyia qamar 2018 年 7 月 28 日
isn't there any easy way? a one line function?
Walter Roberson
Walter Roberson 2018 年 7 月 28 日
figure(1);
plot(Iteration, total_util*100, 'b', Iteration, DD, 'r', Iteration, PP, 'g');
legend('total_util','d_quantity','Price');
figure(2);
plot(total_util);

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

カテゴリ

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

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by