MATLAB Answers

0

Indexing error when using the plot function.

Asked by Calvin Chang on 24 Jun 2019
Latest activity Edited by Adam Danz
on 24 Jun 2019
Super basic, but what is wrong with the code below? I'm not understanding. Multiple tutorials state that the code below should be perfectly fine? But I end up receving the error that is shown below the code.
x=[0 1 2 3 4];
y=[2 9/4 38/17 161/72 682/305];
plot(x,y)
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in test (line 4)
plot(x,y)

  0 Comments

Sign in to comment.

1 Answer

Answer by madhan ravi
on 24 Jun 2019

clear all
% you have a variable named plot

  1 Comment

+1 To add on to Madhan's answer, you can confirm that by running this line
which plot
which will return "plot is a variable". You should never name a variable "plot".
This recreates your error
plot = 1:4; %variable named plot (gasp!)
%now treat plot as a funciton
x=[0 1 2 3 4];
y=[2 9/4 38/17 161/72 682/305];
plot(x,y)
"Index in position 1 is invalid....."

Sign in to comment.