Unkown Source of "Index in position 1 exceeds array bounds" error in simple line plot.
Show older comments
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
Stephen23
on 3 Feb 2019
@Jordan Brooks: please show us the complete error message. This means all of the red text.
Image Analyst
on 3 Feb 2019
I agree with Stephen, since the code works and makes this figure:

Perhaps you've called your script plot.m??? What does this say:
>> which -all plot
Accepted Answer
More Answers (1)
Jordan Brooks
on 3 Feb 2019
0 votes
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!