For Programmers: Free Programming Magazines  


Home > Archive > Matlab > December 2005 > How to label points in plot function?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to label points in plot function?
Mohammed Alharbi

2005-10-09, 7:07 pm

Suppose I have some points in x,y space ....
and I used plot(x,y,'*') to plot my points.
Now, how can I label those points?
I want to label each point with a number.

thanks,
Mohammed
per isakson

2005-10-09, 7:07 pm

Mohammed Alharbi wrote:
>
>
> Suppose I have some points in x,y space ....
> and I used plot(x,y,'*') to plot my points.
> Now, how can I label those points?
> I want to label each point with a number.
>
> thanks,
> Mohammed


text(x,y,'string') adds the string in quotes to the location
specified by the point (x,y).

/ per
Mohammed Alharbi

2005-10-09, 9:59 pm

per isakson wrote:
>
>
> Mohammed Alharbi wrote:
>
> text(x,y,'string') adds the string in quotes to the location
> specified by the point (x,y).
>
> / per

Your code will lebel only 1 point with only 1 label ..
what I want is to label all the points at once with numbers.
So, the first point will be labeled as 1
the second point will be labeled as 2 ... and so on.
I tried to use the following
text(A,B,[1:6]) where A contains x values and B contains y values.
But that didn't work.

thanks
per isakson

2005-10-09, 9:59 pm

Mohammed Alharbi wrote:
>
>
> per isakson wrote:
> Your code will lebel only 1 point with only 1 label ..
> what I want is to label all the points at once with numbers.
> So, the first point will be labeled as 1
> the second point will be labeled as 2 ... and so on.
> I tried to use the following
> text(A,B,[1:6]) where A contains x values and B contains y values.
> But that didn't work.
>
> thanks


Whats wrong with reading the manual or the on-line help? See:
1. for
2. num2str

/per
Mohammed Alharbi

2005-10-09, 9:59 pm

per isakson wrote:
>
>
> Mohammed Alharbi wrote:
location[color=darkred]
> values.
>
> Whats wrong with reading the manual or the on-line help? See:
> 1. for
> 2. num2str
>
> /per


I know how to use for loop ... but I think we can do many things
without using for loop.
The problem of using for loop that it is too slow, because it takes
O(n) to run.( I have taken algorithm design course and I know this
fact).
thanks,
Mohammed
Jérôme

2005-10-10, 3:59 am

Hi,

it seems that <per> forgot to mention :

help text

It returns this :
"If 'string' is an array the same number of rows as the
length of X and Y, TEXT marks each point with the corresponding
row
of the 'string' array."

See example there :
BoB, "How i can plot points coordinates?" #, 31 Aug 2005 5:52 am </WebX?50@@.ef12924>


Jérôme
landon

2005-12-14, 4:04 am

Jérôme wrote:
>
>
> Hi,
>
> it seems that <per> forgot to mention :
>
> help text
>
> It returns this :
> "If 'string' is an array the same number of rows as the
> length of X and Y, TEXT marks each point with the corresponding
> row
> of the 'string' array."
>
> See example there :
> <a href="/WebX?50@@.ef12924">BoB, "How i can plot points

coordinates?" #, 31 Aug 2005 5:52 am</a>
>
>
>
> Jérôme


that's great but i need to label random points that are not stored in
an array. E.g., i have generated 5 random points (stored in a struct
along with some other info) and plotted them on a 100x100 grid and
need to label them with their sequence number. when i use something
like;

text(point(i).x,point(i).y,'label')

i get the "invalid parameter/value pair arguments." error.

it seems that 'text' won't allow real-time calculations of coordinate
points because raw integers work fine. now, I've seen all the posts
dealing with arrays, but they are always in a sequence [1...x], which
does work.

i have also tried the alternate syntax;

text('Position',[x,y],'label' but that doesn't work either.

thanks.
Jérôme

2005-12-14, 4:04 am

Hi,

could you post more lines from your code ?

I don't understand the problem

Jérôme
Jos

2005-12-14, 4:05 am

landon wrote:
>
>
> Jérôme wrote:
> coordinates?" #, 31 Aug 2005 5:52 am</a>
>
> that's great but i need to label random points that are not stored
> in
> an array. E.g., i have generated 5 random points (stored in a
> struct
> along with some other info) and plotted them on a 100x100 grid and
> need to label them with their sequence number. when i use something
> like;
>
> text(point(i).x,point(i).y,'label')
>
> i get the "invalid parameter/value pair arguments." error.
>
> it seems that 'text' won't allow real-time calculations of
> coordinate
> points because raw integers work fine. now, I've seen all the
> posts
> dealing with arrays, but they are always in a sequence [1...x],
> which
> does work.
>
> i have also tried the alternate syntax;
>
> text('Position',[x,y],'label' but that doesn't work either.
>
> thanks.


You have to transform you positions into a single array.

% some structure data
for i=1:3,
p(i).x = rand(1) ;
p(i).y = rand(1) ;
end
Labels = {'A','B','C'} ;

text(cat(1,p(:).x),cat(1,p(:).y),Labels)

hth
Jos
landon

2005-12-15, 4:02 am

Jos, i converted the code to store the positions in arrays. here it
is;

------------------------------------------------
for i = 1:sensors_per_team;

plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
x_pos(i) = team(i).x;
y_pos(i) = team(i).y;
label(i) = {['sensor',int2str(i)]};

end

text(x_pos,y_pos,label);
------------------------------------------------
This didn't work either. i got the same error; "error using text;
invalid parameter/value pair assignments". when i ran in debug mode,
all three arrays, x_pos, y_pos and label, are correct (same
dimension, correct values etc). i also tried the concatenated
version of specifying parameters in the "text(...)" line.

Jerome, hope this helps clarify the problem. basically, "team" is an
array of struct with team(1) representing a sensor on a map e.g. it
has x & y positions as well as a frequency range it can detect at.

i plot all the sensors in the code above (which works fine) and now
need to label them with a string (specifically, the frequency range).

thanks for the help.
Jérôme

2005-12-15, 4:02 am

Hi,

I didn't take my first coffee... but :

what is the dimension of team(i).x ?

Try this :

for i = 1:sensors_per_team;

plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
text(team(i).x,team(i).y,sprintf('sensor%d',i));

end

In your first post you made a mistake :

text(point(i).x,point(i).y,'label')

should be

text(point(i).x,point(i).y,label)

I take my first coffee ;)
Jérôme
landon

2005-12-16, 4:00 am

Jérôme wrote:
>
>
> Hi,
>
> I didn't take my first coffee... but :
>
> what is the dimension of team(i).x ?
>
> Try this :
>
> for i = 1:sensors_per_team;
>
> plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
> text(team(i).x,team(i).y,sprintf('sensor%d',i));
>
> end
>
> In your first post you made a mistake :
>
> text(point(i).x,point(i).y,'label')
>
> should be
>
> text(point(i).x,point(i).y,label)
>
> I take my first coffee ;)
> Jérôme


Jerome,

this was the way i coded it at first but got the same error as all
the other times (invalid parameter/value pair...). i have tried
various ways to pass the sensor's plot position to the text function
but they do not work. the sprintf version above results in the same
error.

i would like to know exactly what the error message means;
--------------------------------------
??? Error using ==> text
Invalid parameter/value pair arguments.
--------------------------------------
again, when i run in debug, the terms for the positions evaluate
correctly when i check before letting the 'text...' line run.


the dimension of team(i) is 6 in the current implementation but it
can be set at the beginning of the program.

thanks.
Jérôme

2005-12-16, 4:01 am

It seems that I can't understand the problem...

try this :

figure

team(1).x=rand(5,1);
team(1).y=rand(5,1);
team(2).x=rand(5,1);
team(2).y=rand(5,1);

hold on

for i = 1:2

plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
text(team(i).x,team(i).y,sprintf('sensor%d',i));

end

Does it work ?
Jérôme
landon

2005-12-17, 3:59 am

Jérôme wrote:
>
>
> It seems that I can't understand the problem...
>
> try this :
>
> figure
>
> team(1).x=rand(5,1);
> team(1).y=rand(5,1);
> team(2).x=rand(5,1);
> team(2).y=rand(5,1);
>
> hold on
>
> for i = 1:2
>
> plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
> text(team(i).x,team(i).y,sprintf('sensor%d',i));
>
> end
>
> Does it work ?
> Jérôme


Yes it does work!!?? but i don't understand why it does.
landon

2005-12-17, 3:59 am

landon wrote:
>
>
> Jérôme wrote:
> plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
>
> Yes it does work!!?? but i don't understand why it does.


i also tried this (which didn't work either);
------------------------------
for i = 1:sensors_per_team;

plot(team(i).x,team(i). y,'s','MarkerSize',9,'MarkerFaceColor','
k');
xcoord = team(i).x;
ycoord = team(i).y;
text(xcoord,ycoord,'sensor');
end
------------------------------
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com