I need to plot a vertical text (text 2) next to a horizontal text (text 1), if I use the the same position that I used to plot text1 they become superimposed.
I've tried to infer the second position based on the Extent property of text1, but I can't get the units right:
rec = txt.Extent;
pos_x = rec(1) + rec(3);
pos_y = rec(2);
text(pos_x,pos_y,txt2,'HorizontalAlignment','center','FontSize',sz,'Rotation',90,'Units','normalized');
I've tried also with units in pixels but that didn't worked either.
The figure is a time series, I couldn't find any convertion function.
From the documentation it seems to me that the Extent values are normalized but I dont know if anything else is needed to display the second position on those coordinates.
Any pointers?? (hehe...get it?)

 채택된 답변

Star Strider
Star Strider 2020년 8월 12일

0 개 추천

I am not certain what the problem is.
Try something like this:
x = 1:20;
y = 10*sin(pi*x/5);
figure
plot(x, y)
grid
vtext = compose('%s\n%s\n%s\n','Vertical','text','example');
htext = compose('%s', 'Horizontal text example');
text(6,5,vtext,'VerticalAlignment','middle', 'HorizontalAlignment','right')
text(6,5,htext, 'HorizontalAlignment','left')
.

댓글 수: 2

juan pedrosa
juan pedrosa 2020년 8월 12일
The problem happens when both texts are centered horizontally, I see that your solution is better and I'll use it, but I wonder, shouldn't I be able to infer based on the extent position of text1 the position for text2?
It shouldn't need to be more complicated than my code right? If not then maybe I misunderstood the documentation or how the relative coordinates are calculated.
If they are both centered horizontally, I would use 'VerticalAlignment','top', for the vertical one, and 'VerticalAlignmment','bottom' for the horizontal one.
In my code, the text calls then become:
text(6,5,vtext,'VerticalAlignment','top', 'HorizontalAlignment','center')
text(6,5,htext,'VerticalAlignment','bottom', 'HorizontalAlignment','center')
When I ran that just now (with the rest of my code unchanged), it appeared to produce the desired result, at least as I understand what you want. Note that the coordinates and 'HorizontalAlignment' name-value pair arguments are the same for both text calls, with only the 'VerticalAlignment' name-value pair arguments different.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2020년 8월 12일

댓글:

2020년 8월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by