Error encountered when attempting to use Text()

2 views (last 30 days)
Hi, I'm relatively new to Matlab. I've been trying to annotate some specific points in my graph but keep encountering this error: "Subscript indices must either be real positive integers or logicals.". This error appears even when I used the example code (see below). I noticed that if I start my command window fresh, the example code works fine, but as soon as I load my data, the error appears again both when I plot my own data and when I use the example code. Any idea as to why this is the case? Thanks
x = 0:pi/20:2*pi;
y = sin(x);
figure % new figure window
plot(x,y)
text(pi,0,' \leftarrow sin(\pi)')
Subscript indices must either be real positive integers or logicals.

Accepted Answer

dpb
dpb on 25 Jan 2015
Edited: dpb on 25 Jan 2015
Looks like he defined an array named text in which case he's aliased the builtin text function. Using a set of subscripts looking like those for the function would, indeed, be trying to use a text string for at least one of those which would raise the error...
>> text=rand(3);
>> text(0,0,'Hello world')
Subscript indices must either be real positive integers or logicals.
>>
Moral: Don't do that! :)
Rename the array to something that doesn't clash with any Matlab-supplied function.
Text, txt, ..., etc., ...
  1 Comment
Dzung Le
Dzung Le on 25 Jan 2015
Thank you so much for your quick response! That is exactly my mistake! :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 25 Jan 2015
It works fine for me - no errors. And I can run it over and over again with no errors. Perhaps you used Text() (like you said in your subject line) instead of text(). MATLAB is case sensitive.

Tags

Community Treasure Hunt

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

Start Hunting!