Error message when using plot command: "Subscript indices must either be real positive integers or logicals"

I am having an issue when I run this code:
x = linspace(0,1,7);
yprimeexact = -2*x.*exp(-x.^2);
yprimeapprox = [-1.3264656E-02 -0.3154821 -0.5814109 -0.7609767 -0.8383472 -0.8199028 -0.7577648];
plot(x,yprimeexact,'b')
hold on
plot(x,yprimeapprox,'r')
When I try to plot the data, I get an error message saying, "Subscript indices must either be real positive integers or logicals." Do you know what is wrong? Thank you in advance!

1 件のコメント

Looks like you've inadvertently created an array named plot earlier...
clear plot
then try again. If that doesn't cure it, do
which plot
and post it and the full error in context but I suspect it will...

サインインしてコメントする。

回答 (2 件)

The above code should execute properly Kelsey. Perhaps you have a variable named linspace() in your workspace?
If you execute
>>who linspace
do you get something back, like "Your variables are"
If so try
>> clearvars linspace
and try again. Worse case, just clear everything and copy and paste the above code back into the workspace, it should work.

4 件のコメント

I tried all of those things, but unfortunately to no avail. I even tried changing the x vector to this:
x = [0 0.1667 0.3333 0.5000 0.6667 0.8333 1.0000];
but I still get the same error message...
Post the full error and the source in context--we can't see your command window from here
Perhaps also post the output of
whos
after you've done a clear and tried again and had the same problem.
If, of course, it still happens there's always the case of exiting Matlab completely and starting over to see if perhaps Matlab has gotten internally confused...
Could also be a shadowed plot.m function which inside does indexes into x or similar.
dpb
dpb 2013 年 10 月 10 日
which was my first suggestion of likely cause but seems if OP really did what says did (and didn't do the wrong thing again right after which we can't see) should've gone away.
But, then again, if is running a script that has the problematic line in it and we're not seeing that, then it's quite possible that the problem gets fixed at the command line then runs the script that has the error and makes the problem all over again-- ad nauseum, so to speak... :)

サインインしてコメントする。

Run:
dbstop if error
At the command line.
Then run the above code. It will stop with the debugger on the offending line and you will be able to see which function (e.g. exp, plot, linspace has a variable named after it or another function shadowing the builtin one.

カテゴリ

質問済み:

2013 年 10 月 10 日

コメント済み:

dpb
2013 年 10 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by