ERROR message when running code

1 回表示 (過去 30 日間)
Amr Mousa
Amr Mousa 2019 年 4 月 16 日
コメント済み: Adam Danz 2019 年 4 月 19 日
The following command only plots the first subplot and gives the ERROR message:
Error in subplot (line 8)
subplot(3,1,1);
and this is the code below:
clear all
clc
x = 0 : pi/150 : 2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);
subplot(3,1,1);
plot(x, y1, 'r')
subplot(3,1,2);
plot(x, y2, 'g')
subplot(3,1,3);
plot(x, y3, 'b')
grid off
xlabel('Time (sec)');
ylabel('Amp (v)');
title('Signal xt');
legend('y1', 'y2', 'y3');
  2 件のコメント
Adam Danz
Adam Danz 2019 年 4 月 16 日
The code works fine for me, without error. The only problem I see is the legend() does not specify object handles and therefore only shows one of the three lines.
Please copy-paste the entire error message and tell us what version of matlab you're running. Run 'ver' (without quotes).
Amr Mousa
Amr Mousa 2019 年 4 月 19 日
My friend, I have a problem with "subplot" itself.
look, I tried to get the figure for one "subplot" as shown below and the problem still appears.
Code::
clear
clc
x = 0:pi/150:5;
y = sin(5*x);
subplot(1, 1, 1);
plot(x, y, 'r');
and I still have such an error
Attempt to execute SCRIPT subplot as a function:
subplot(1, 1, 1);

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

採用された回答

Star Strider
Star Strider 2019 年 4 月 16 日
編集済み: Star Strider 2019 年 4 月 19 日
Your code plots all three variables. Note that they only differ by phase, so the easiest way to detect the difference is where they begin at ‘x=0’.
The error is not an error. It is a Warning, because you are plotting one variable and the legend specifies three variables. You either need to use a separate legend call in every subplot, or instead use the title function with each subplot.
EDIT —
In your Comment you mention the error is:
‘Attempt to execute SCRIPT subplot as a function:’
You have a script that you named ‘subplot’. This is called ‘overshadowing’ a MATLAB function. Please do not do that!
The solution is to re-name your script to something that does not have the same name as a MATLAB function. For example rename it to ‘mySubplot.m’ or something similar that makes sense in the context of your code.
  1 件のコメント
Adam Danz
Adam Danz 2019 年 4 月 19 日
+1
@ Amr Mousa, this should address all of your questions.

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

その他の回答 (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