ERROR message when running code

조회 수: 2 (최근 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 CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by