Unkown Source of "Index in position 1 exceeds array bounds" error in simple line plot.

Hello all,
I've recently switched from Python to Matlab for a college Thermo class, and this is our first real coding assignment. I've got an errorbarplot that I would like to fit a line to, but I've been getting "Index in position 1 exceeds array bounds (must not exceed 1)." from the line containing "plot(x,y,'--')" in the following code. I found the polynomial fit using the curve fitting app, then just defined a function for the polnomial with the fit constants, to plot. I understand indexing, but I can't see why the array bounds of the plot function "must not exceed 1". I've tried quite a few different modifications, and nothing I've done seems to fix anything. I'd appreciate some community debugging.
x = linspace(1,4,4)
%also tried x = [1 2 3 4]
%and x = [1;2;3;4]
%But I get the same error every time
figure
p1 =1.965;
p2 = 0.01964;
y = p1*x + p2;
hold on
plot(x,y,'--')
title('Recurrence Time vs. Number of Particles')
xlabel('Particle Number')
ylabel('Recurrence Time')
hold off

2 Comments

@Jordan Brooks: please show us the complete error message. This means all of the red text.
I agree with Stephen, since the code works and makes this figure:
0001 Screenshot.png
Perhaps you've called your script plot.m??? What does this say:
>> which -all plot

Sign in to comment.

 Accepted Answer

clear plot % at the very beginning plot seems to be a variable stored in the workspace

2 Comments

Jordan Brooks's answer moved here:
Tried to add "clear plot" to the beggining of the code, didn't fix it unforunately.

Sign in to comment.

More Answers (1)

The problem was that I had named another script plot.m and a function plot = something, and hadn't cleared the workspace, I think. Following that suggestion, I renamed everything from "plot", and cleared the workspace, now it works as expected. Thanks for the help all.

Community Treasure Hunt

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

Start Hunting!