Text position on right or left yaxis in app designer

7 views (last 30 days)
Hello,
I work on an applicaton in app designer which display several plots on left and right Y axis, I added a cursor function which allow to display data of each plot by using text function.
My problem is that I can not control the position of the text.
Indeed, the text position should be always set according to the left yaxis. However, when ylim of the right Yaxis is set on 'manual', the text position is set according to the right Y axis, which is not desired.
I tried yyaxis 'left' function but it does not work, neither 'Yaxislocation'. It should be set on properties somewhere but I can not find it.
Any help would be welcome. Many Thanks
% other code lines
allLines = findobj(gcf, 'type', 'line');
% other code lines
for p=1:length(IndLines)
hText(p) = text(NaN, NaN, '', 'Parent', get(allLines(IndLines(p)), 'Parent'), 'BackgroundColor', [0.94 0.94 0.94], 'Color', get(allLines(IndLines(p)), 'Color'));
end
% other code lines
for p=1:length(IndLines)
ydata = get(allLines(IndLines(p)), 'Ydata');
y = interp1(xdata, ydata, pt(1));
set(hText(p), 'Position', [Xlimite(1)+((Prop(p)*(RXlim))/100), Ymain(1) + (0.03*RYmain)], 'String', sprintf('(%0.01f, %0.2f)', pt(1), y)); % Pour gérer les chiffres après la virgule
end
% other code lines
  3 Comments
dpb
dpb on 6 May 2021
yyaxis is handy for the simple interactive use; it's a pit(proverbial)a(ppendage) when try to do more things with it that it doesn't return addressable handles that are unique for the L/R sides, agreed.
"Everything should be made as simple as possible, but no simpler.” -- Einstein
Sometimes TMW tries to simplify too much for user convenience and makes more work than they save. I think this syntax/operation is one in which is true. Granted, the interface/trivial use is much neater than venerable plotyy, but you've lost the facility to retrieve the desired axes handle and have to go through the gyrations of trying to ensure it is the correct one with focus if anything is done out of sequence and you can't inquire to find out because gca returns same handle irrespective of which is the current y-axis internally.
>> figure
>> plot(rand(4,1))
>> hAx=gca;
>> yyaxis right
>> plot(randn(5,1))
>> gca==hAx
ans =
logical
1
>>
So, your attempt to retrieve the handle of the axis fails to help because both y-axis objects are now children of the axes instead of two entirely different and overlaying axes as in plotyy.
>> hAx.YAxisLocation
ans =
'right'
>> yyaxis left
>> hAx.YAxisLocation
ans =
'left'
>> ylabel('Left')
>> hAx.YAxisLocation='right';
The YAxisLocation property is read-only. Use yyaxis to select a different y-axis.
>>
You can check that you are addressing the side you want, but you can't change the target that way, only yyaxis has necessary access to the internals to change that focus.
Hence the need to save the handles -- your code to retrieve them would be ok other than generally it's more work to retrieve than it is to just save in the beginning, but you then have to use the right handle in a call to yyaxis to ensure have the target where want it.

Sign in to comment.

Answers (1)

Marc Servagent
Marc Servagent on 12 May 2021
Thanks, I could finally solve my problems by keeping left and right yaxis as different axes and not children of the axes.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!