coordinate data in the text command

Is it possible to somehow put both, x- and y-coordinate, with a single variable of two fields into the location fields of the text-command?
instead of :
loc = [0.5,0.5];
figure; text(loc(1),loc(2),'a')
something like
Is it possible to somehow put both, x- and y-coordinate, into the location fields of the text-command?
instead of :
loc = [0.5;0.5];
figure; text(loc,'a')

 Risposta accettata

Rik
Rik il 16 Dic 2021
You have two options that I'm aware of:
  1. Write a wrapper function that will split your vector as approriate.
  2. Use a comma separated list: loc=[0.5;0.5];loc=num2cell(loc);text(loc{:},'a')

13 Commenti

Hi Rik
Thank You, Your suggestion using loc{:} instead of just loc within the text-command was the crucial point. I'm still a bit confused about the formatting of cell arrays, what the command expects and why the command accept this but not that.
I now use the following syntax
loc = {0.5;0.5}
figure
text(loc{:},'a')
{:} will generate a comma separated list. That means these two lines are equivalent:
%loc{:}
%loc{1},loc{2}
That is why this syntax works. Your single input isn't actually a single input as far as the text function is concerned.
The syntax Walter proposed works because it uses the Name,Value syntax that most objects (and many functions) in Matlab allow. You specify the 'Postition' parameter as a vector, just like the documentation describes. Since you're skipping the normal syntax options (the first two you see below), you need to specify the 'String' parameter as well (which, confusingly, doesn't need to contain a string, but may contain a char array instead, which is because the parameter name predates the data type).
help text
TEXT Add text descriptions to data points TEXT(x,y,str) adds a text description to one or more data points in the current axes using the text specified by str. To add text to one point, specify x and y as scalars in data units. To add text to multiple points, specify x and y as vectors with equal length. TEXT(x,y,z,str) positions the text in 3-D coordinates. TEXT(...,Name,Value) specifies text properties using one or more Name,Value pair arguments. For example, 'FontSize',14 sets the font size to 14 points. You can specify text properties with any of the input argument combinations in the previous syntaxes. If you specify the Position and String properties as Name,Value pairs, then you do not need to specify the x, y, z, and str inputs. TEXT(container,...) creates the text in the axes, group, or transform specified by container, instead of in the current axes. T = TEXT(...) returns one or more text objects. Use T to modify properties of the text objects after they are created. For a list of properties and descriptions, see Text Properties. You can specify an output with any of the previous syntaxes. Execute GET(T), where T is a text object, to see a list of text object properties and their current values. Execute SET(T) to see a list of text object properties and legal property values. See also XLABEL, YLABEL, ZLABEL, TITLE, GTEXT, LINE, PATCH. Documentation for text doc text Folders named text io/text
hans
hans il 16 Dic 2021
Modificato: hans il 16 Dic 2021
Thank You for Your explanation. That makes things more transparent.
Reading the Matlab helpfile, in some case cases does not make things clearer to me. Of course I had read the helpfile before and it didn't answer my question
  • It did not explain, that one vector with two elements in the first position did not cover the requirements of text
  • It did not explain, that {:} makes comma separated numbers that were exceptable to the command.
The required information, I didn't know, is too basic knowledge I guess. Thank You for sharing.
Regarding your first point: it should have. It clearly states that the first argument is the x-coordinate and the second the y-coordinate if you use the first syntax.
Comma separated lists are part of the general Matlab syntax and should be explained in a course/tutorial in the context of struct and cell. They don't have anything to do with the text function directly. This isn't basic knowledge, but it is not directly related. If you want more information, I encourage you to have a look at the documentation page and the pages it links to. I just noticed it doesn't mention structs, but those work with the same principle:
s.a=1;s(2).a=2;s(3).a=rand;
s.a %this returns a comma separated list
ans = 1
ans = 2
ans = 0.6719
%using [] to create an array is effectively calling the horzcat function
[s.a]
ans = 1×3
1.0000 2.0000 0.6719
Thank You
yes, it's a wide field of knowledge.
It is not obvious for me, that >>s.a %this returns a comma separated list<< , as the answer of the program shows
  • ans = 1
  • ans = 2
  • ans = 0.6719
I just realized by accidence, that it doesn't make a difference in a structure variable, where I place the index-count.
>> s.a(1)=1; s(2).a=2; s(3).a=rand
s.a returns a comma separated list, just like loc{:} did, so it equivalent to:
s(1).a , s(2).a , s(3).a
If you execute that line of code, what do you expect the output to be? Exactly: three lines of ans.
As for your last remark: yes it does. You code only differs from mine in how you define the first element. Since a(1)=1 is the same as a=1 (as long as a doesn't exist yet), the result is the same. If you did s.a(3)=rand; the result would be different:
s.a(1)=1
s = struct with fields:
a: 1
s(2).a=2
s = 1×2 struct array with fields:
a
s.a(3)=rand
Scalar structure required for this assignment.
Stephen23
Stephen23 il 16 Dic 2021
Modificato: Stephen23 il 16 Dic 2021
"I just noticed it doesn't mention structs"
"it doesn't make a difference in a structure variable, where I place the index-count.."
Yes, it does: indexing into the field array is totally different to indexing into the structure array.
hans
hans il 16 Dic 2021
Modificato: hans il 16 Dic 2021
I get the this identical output for my both versions.
>> clear s; s(1).a = 1
s =
struct with fields:
a: 1
>> clear s; s.a(1) = 1
s =
struct with fields:
a: 1
But You are right, this is only the case for index number 1
But how could I recognize that
  • s.a returns a comma separated list, just like loc{:} did, so it equivalent to:
  • s(1).a , s(2).a , s(3).a
  • If you execute that line of code, what do you expect the output to be? Exactly: three lines of ans.
means a comma separated list in the nomenclature of Matlab?
Stephen23
Stephen23 il 16 Dic 2021
Modificato: Stephen23 il 16 Dic 2021
Thank You for this link:
What I think, I understand is: The "comma separated" is a special communication form for writing and reading with cell arrays
  1. a comma separated list of variables assigned to a cell array, will assign each variable between each of the commas into a separate element of the cell array e.g. a={1,2,3,....}
  2. a cell array inserted into a Matlab command will insert all cells in a row, as a comma separated list of its elements
Stephen23
Stephen23 il 16 Dic 2021
Modificato: Stephen23 il 16 Dic 2021
"The "comma separated" is a special communication form for writing and reading with cell arrays
No, a comma separated list is a list of variables separated by commas.
They can be generated by writing them explicitly, or from a structure, or from a string array, or from a cell array.
Your usage of the terms "writing and reading" is not clear to me. The terms reading/writing are usually applied only to disk IO or similar actions, which comma-separated lists have nothing (directly) to do with.
hans
hans il 16 Dic 2021
Modificato: hans il 16 Dic 2021
"Your usage of the terms "writing and reading" is not clear to me. "
  • I used writing for may be a better word might be "assigning a value to a variable" e.g. a=1
  • I used reading for replacing the "value of a variable" by the "variable"
"No, a comma separated list is a list of variables separated by commas."
That is obviously correct. But I try to talk about the field of application of this list.
So may be it is more suitable to modify my statement by ""The "comma separated" is a special communication form for assigning values to arrays and using these arrays to replace a comma separated list of arguments, which was expected in a command"
My point is, that the application of a comma separated list is only in the communication i.e. assigning and use of arrays
May be to tell it shorter:
It seems to me comma separated lists have their dominating area of application assigning values to arrays and to fill argument lists of commands/functions

Accedi per commentare.

Più risposte (1)

text('String', 'a', 'Position', loc)

1 Commento

hans
hans il 16 Dic 2021
Modificato: hans il 16 Dic 2021
Thank You for Your contribution.
I'm still confused, why the text command accept this syntax of loc after the 'Position' switch, but not as the first entry of the text command
Why does the first position require a cell variable
the second requires a vector
loc = {0.5;0.5}
figure
text(loc{:},'a')
clear
loc = [0.5 0.5]
figure
text('Position', loc, 'String','a');

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Prodotti

Release

R2021b

Richiesto:

il 16 Dic 2021

Commentato:

il 16 Dic 2021

Community Treasure Hunt

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

Start Hunting!

Translated by