error using text()

I want to put a text on a plot using the following code:
x_pos=0.0031;
y_pos=0.0507
str_=['R^2 (%) = ' num2str(R_squared)];
text(x_pos,y_pos , str_)
it used to work but now it refuses to work and gave me the following error:
??? Error using ==> text
Not enough input arguments.
However, if I use the following code, it will work:
text(0.0031, 0.0507 , str_)
I don't know what the problem is. Could you please help me about that?

 Risposta accettata

Matt Fig
Matt Fig il 27 Apr 2011

1 voto

Works here:
clear all,close all,clc
R_squared = .5;
x_pos=0.0031;
y_pos=0.0507;
str_=['R^2 (%) = ' num2str(R_squared)];
text(x_pos,y_pos , str_)
I see some text on an axes...
%
%
%
EDIT
I see what you mean now. I get the same error when y is single. If x is single, I get a different error. As I don't see this documented anywhere, I wonder if it is a bug. For now, doing this will make sure you get your text:
text(double(x_pos),double(y_pos),str_)
%
%
%
%
EDIT#2
I think this is buggy. This works with singles, but the other calling format, see above, doesn't:
R_squared = .5;
x_pos=single(0.0031);
y_pos=single(0.0507);
str_=['R^2 (%) = ' num2str(R_squared)];
text('position',[x_pos,y_pos] ,'string',str_)

11 Commenti

Hassan
Hassan il 27 Apr 2011
thnaks Matt for the comment. I did that but I still get the same error. the type of x_pos is double and y_pos is single. I converted x_pos to single to see if it fix the error but it didn't.
Matt Fig
Matt Fig il 27 Apr 2011
Try this:
text(double(x_pos),double(y_pos) , str_)
Matt Fig
Matt Fig il 27 Apr 2011
When I pass a single value for either x or y or both I get an error.
If only y is single, I get the error about not enough inputs.
If only x is single, I get invalid property/value pair.
Is this a bug?
Hassan, what MATLAB version are you using?
Hassan
Hassan il 27 Apr 2011
if I use the same thing you did I don't get an error but if I use my own data I'll get the error. Here's a subset of my data:
X=[0.0317197366079450
0.0164593809477460
0.00843903002655300
0.0104984758966280
0.00635047132957800]
y=[0.01150000
0.005500000
0.004000000
0.009400000
0.006900000]
R_squared=0.500000000000000
x_pox=0.00133576536015362
y_pos=0.01006250
str_= 'R^2 (%) = 0.5'
scatter(X,Y)
text(x_pos,y_pos , str_)
Hassan
Hassan il 27 Apr 2011
I'm using Matlab 2009a.
Hassan
Hassan il 27 Apr 2011
It confused me a lot. I thought there's something wrong with the code. thanks a lot Matt.
Matt Fig
Matt Fig il 27 Apr 2011
Did you see my previous comment, and above edits?
You said that y_pos was single, but the above code you paste does not show this. In any case, see my above edits and see if that helps.
Hassan
Hassan il 27 Apr 2011
yes Matt, I saw your comments. I changed x_pos and y_pos to double, as you suggested, and now it's working fine. I just copied the value for x_pos and y_pos from a subset of my data and from Workspace window, the data type may have changed due to that.
Matt Fig
Matt Fig il 27 Apr 2011
O.k., I still think this could be a bug.
Hassan
Hassan il 27 Apr 2011
you mean it's an error caused by Matlab? I'm quite new to Matlab and if I see an error it takes time to figure out the problem.
Matt Fig
Matt Fig il 27 Apr 2011
By bug I mean:
Most MATLAB functions nowadays work with either single or double.
The documentation for TEXT does not specify that the inputs must be of type double.
One calling format works fine with single values, the other doesn't.
If this is not a bug, it is at least inconsistent.

Accedi per commentare.

Più risposte (2)

Chethan Pandarinath
Chethan Pandarinath il 31 Dic 2013

0 voti

Just wanted to confirm that I see this issue in MATLAB R2012b: text() fails if either of the passed in x- or y-position arguments is a single (and it fails with a non-sensical "Not enough input arguments" error). Casting x- and y-position to double fixes the problem.
Agreed with Matt that this should be marked as a bug, as there is nothing in the documentation specifying that the x and y position arguments must be doubles.
Thank you for your post and for the workaraound.
Sumana
Sumana il 27 Nov 2023

0 voti

My code is:
for i = 1:length(props)
% Extract bounding box and centroid information
boundingBox = props(i).BoundingBox;
centroid = props(i).Centroid;
% Text to be displayed
textToDisplay = sprintf('Object %d', i);
% Add text to the image
text(centroid(1), centroid(2), textToDisplay, 'Color', 'r', 'FontSize', 10, 'FontWeight', 'bold');
end
i am supposed to receive an image where bounding box and centroid will be displayed, but i am getting a white image having x and y axis, please tell me the reason

Categorie

Scopri di più su Deep Learning Toolbox in Centro assistenza e File Exchange

Tag

Richiesto:

il 27 Apr 2011

Risposto:

il 27 Nov 2023

Community Treasure Hunt

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

Start Hunting!

Translated by