How to set text position in yyaxis?

5 views (last 30 days)
galaxy
galaxy on 12 Oct 2022
Edited: dpb on 13 Oct 2022
Hi all
I tried to set text position as following. It was OK.
t = 1:0.1:10;
yyaxis('left');
plot(t, sin(t), 'b.-', 'LineWidth', 2);
ylabel('sin');
hold on
yyaxis('right');
plot(t, cos(t)*2, 'g.-', 'LineWidth', 2);
ylabel('cos');
xlabel('Time[ms]');
grid on;
yyaxis('left');
text(4, 0.5,'left=0.5') %I would like this to be referenced to the 2nd y-axis
yyaxis('right');
text(4, 0.5,'right=0.5') %I would like this to be referenced to the 1st y-axis
hold off
But now I want to change text by set
t = 1:0.1:10;
yyaxis('left');
plot(t, sin(t), 'b.-', 'LineWidth', 2);
ylabel('sin');
hold on
yyaxis('right');
plot(t, cos(t)*2, 'g.-', 'LineWidth', 2);
ylabel('cos');
xlabel('Time[ms]');
grid on;
hText = nan(1, 2);
for cnt = 1:2
hText(cnt) = text(NaN, NaN, '', ...
'BackgroundColor', 'yellow');
end
yyaxis('left');
set(hText, 'Position', [4, 0.5], 'String', 'left=0.5');
%text(4, 0.5,'left=0.5') %I would like this to be referenced to the 2nd y-axis
yyaxis('right');
set(hText, 'Position', [4, 0.5], 'String', 'right=0.5');
%text(4, 0.5,'right=0.5') %I would like this to be referenced to the 1st y-axis
hold off
you can see that, can't display [left=0.5] following 1st y-axis.
Do anyone know why? Please explain and tell me how to fix.
thank you

Answers (1)

dpb
dpb on 12 Oct 2022
Edited: dpb on 12 Oct 2022
t = 1:0.1:10;
yyaxis('left');
plot(t, sin(t), 'b.-', 'LineWidth', 2);
ylabel('sin');
text(4,0.5,'left=0.5','BackgroundColor','y');
yyaxis('right');
plot(t, cos(t)*2, 'g.-', 'LineWidth', 2);
ylabel('cos');
xlabel('Time[ms]');
text(4,0.5,'right=0.5','BackgroundColor','y');
yl=ylim;yticks(linspace(yl(1),yl(2),11)) % make grids align both axes
grid on;
Do what needs doing on each axis when there to begin with.
While what you had will work; it's a lot more trouble switching back and forth than finishing the job when there.
The specific problem in your code is the two lines
...
set(hText, 'Position', [4, 0.5], 'String', 'left=0.5');
...
set(hText, 'Position', [4, 0.5], 'String', 'right=0.5');
...
hText is the array of BOTH handles; each call sets the position of both in turn to the same place on the axis presently in focus. So, the last axis to have focus and the call wins.
BTW when allocating space for future graphics objects, instead of
hText = nan(1, 2);
use
hText = gobjects(1, 2);
which is expressly for the purpose.
  4 Comments
galaxy
galaxy on 13 Oct 2022
I need to use function and call other function.
So, I can not use yyaxis('left'); yyaxis('right'); anytime.
But, I think that can use set() at same time by initialize first.
anw, thank you your anwser. This topic will close in here.
dpb
dpb on 13 Oct 2022
Edited: dpb on 13 Oct 2022
What you think and what happens have been demonstrated to not be consonant in results desired...

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!