How to plot two graphs in one subplot?

17 ビュー (過去 30 日間)
Tsz Yan Yeung
Tsz Yan Yeung 2022 年 7 月 24 日
編集済み: Cris LaPierre 2022 年 7 月 25 日
I would like to plot two graphs in one subplot to compare the parameters of two cars. However, the program does not give the expected outcome. It could only generate one graph in the subplots. How should I correct the program? Thanks!
(I would like to have combined plots similar to the image below.)

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 7 月 24 日
編集済み: Cris LaPierre 2022 年 7 月 24 日
The same way you would add 2 lines to a plot that is not create using subplot - by either using hold or the following plot syntax:
Visit the interactive tutorial on plotting in Ch 9 of MATLAB Onramp for more.
x=-3:.1:3;
y1 = sin(x*pi/3);
y2 = cos(x*pi/3);
% option 1
plot(x,y1)
hold on
plot(x,y2)
hold off
xlim([-4,4])
% option 2
figure
plot(x,y1,x,y2)
xlim([-4,4])
  4 件のコメント
Tsz Yan Yeung
Tsz Yan Yeung 2022 年 7 月 24 日
Thanks for your answer. However, my program still does not show the expected output. May I ask how can a call a variable from another .m file for plotting graph?
For exmaple, I would like to use the variable "DispLog" with saved value from Electriccar.m script in the plot() function in another script.
Cris LaPierre
Cris LaPierre 2022 年 7 月 24 日
編集済み: Cris LaPierre 2022 年 7 月 25 日
You call variables from the workspace, not the file. Therefore, any script can create the variable, but it must be in your workspace when you try to plot it. The challenge here is going to be ensuring you design and run both scripts correctly so that it works as intended.
You may also consider saving your variables to a mat file in the first script, then loading them in the second script.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by