- Write a wrapper function that will split your vector as approriate.
- Use a comma separated list: loc=[0.5;0.5];loc=num2cell(loc);text(loc{:},'a')
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
coordinate data in the text command
조회 수: 1 (최근 30일)
이전 댓글 표시
hans
2021년 12월 16일
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')
채택된 답변
Rik
2021년 12월 16일
You have two options that I'm aware of:
댓글 수: 13
hans
2021년 12월 16일
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')
Rik
2021년 12월 16일
{:} 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
2021년 12월 16일
편집: hans
2021년 12월 16일
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.
Rik
2021년 12월 16일
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
hans
2021년 12월 16일
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
Rik
2021년 12월 16일
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
2021년 12월 16일
편집: Stephen23
2021년 12월 16일
"I just noticed it doesn't mention structs"
See also: https://www.mathworks.com/matlabcentral/answers/320713-how-to-operate-on-comma-separated-lists
"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
2021년 12월 16일
편집: hans
2021년 12월 16일
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?
hans
2021년 12월 16일
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
- 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,....}
- a cell array inserted into a Matlab command will insert all cells in a row, as a comma separated list of its elements
Stephen23
2021년 12월 16일
편집: Stephen23
2021년 12월 16일
"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
2021년 12월 16일
편집: hans
2021년 12월 16일
"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
hans
2021년 12월 16일
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
추가 답변 (1개)
Walter Roberson
2021년 12월 16일
text('String', 'a', 'Position', loc)
댓글 수: 1
hans
2021년 12월 16일
편집: hans
2021년 12월 16일
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');
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)