ERROR message when running code

2 views (last 30 days)
Amr Mousa
Amr Mousa on 16 Apr 2019
Commented: Adam Danz on 19 Apr 2019
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 Comments
Adam Danz
Adam Danz on 16 Apr 2019
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 on 19 Apr 2019
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);

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 16 Apr 2019
Edited: Star Strider on 19 Apr 2019
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 Comment
Adam Danz
Adam Danz on 19 Apr 2019
+1
@ Amr Mousa, this should address all of your questions.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!