ERROR message when running code

1 visualización (últimos 30 días)
Amr Mousa
Amr Mousa el 16 de Abr. de 2019
Comentada: Adam Danz el 19 de Abr. de 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 comentarios
Adam Danz
Adam Danz el 16 de Abr. de 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 el 19 de Abr. de 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);

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 16 de Abr. de 2019
Editada: Star Strider el 19 de Abr. de 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 comentario
Adam Danz
Adam Danz el 19 de Abr. de 2019
+1
@ Amr Mousa, this should address all of your questions.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by