Why is my text going in the wrong subplot?

9 Ansichten (letzte 30 Tage)
Wis
Wis am 12 Dez. 2018
Kommentiert: madhan ravi am 12 Dez. 2018
I'm trying to add text to two subplots stacked vertically. The first add is successful. The second adds go into the top subplot instead of the bottom. The horzontal position is correct, as is the vertical as far as I can tell. The text just seems to be the wrong subplot. I'm using the 'Parent' property in the "text()" call, but it seems to be ignored.
To be clear, it behaves the same way without the 'Parent' property.
The plotted curves are correct in the lower subplot.
What am I doing wrong, please?
figure ;
h1 = subplot(2,1,1) ;
plot(M_lag,M_res) ;
title([ 'DLY = ' sprintf('%d',DLY) ]) ;
ax1 = gca ;
ax1.XTick = -(round(length(M_res),-1)+10)/2:50:round(length(M_res)/2,-1)+10 ;
hold on
plot([ M_dly M_dly ],ax1.YLim(2)*[ 0.03 0.16 ],'b') ;
text('String',sprintf('%d',M_dly),'Position',[ M_dly ax1.YLim(2)*0.16 0 ], ...
'HorizontalAlignment','center','VerticalAlignment','bottom','Parent',h1) ;
h2 = subplot(2,1,2) ;
plot(beg_window:end_window,ref_,'b',beg_window:end_window,res_,'m') ;
title('ref + resp wave') ;
ax2 = gca ;
hold on
plot([ peak_idx peak_idx ],ax2.YLim(2)*[ 0.03 0.16 ],'b') ;
plot([ peak_idx+DLY peak_idx+DLY ],ax2.YLim(2)*[ 0.03 0.16 ],'b') ;
text('String',sprintf('%d',peak_idx),'Position',[ peak_idx ax1.YLim(2)*0.16 0 ], ...
'HorizontalAlignment','center','VerticalAlignment','bottom','Parent',h2) ;
text('String',sprintf('%d',peak_idx+DLY),'Position',[ peak_idx+DLY ax1.YLim(2)*0.16 0 ], ...
'HorizontalAlignment','center','VerticalAlignment','bottom','Parent',h2) ;
  5 Kommentare
Wis
Wis am 12 Dez. 2018
The data is moot after Jan's laser-focus answer (thanks again).
For the future, however, how do I go about adding a bunch of data to the post for test purposes? I can't include all the code that generates it. Can I attache a ".mat" save file or something like that?
Thanks.
madhan ravi
madhan ravi am 12 Dez. 2018
yes ofcourse you can press the option insert and then attach your file

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 12 Dez. 2018
Bearbeitet: Jan am 12 Dez. 2018
It is strange, that you use the dimensions of teh first axes ax1.YLim(2) to define the position of a text in the 2nd axes. So maybe the text is displayed in the 2nd axes, but at a position outside the boundaries, such that it occurs anywhere else, which is accidentally in the other axes. So try this:
text(ax2, 'String', sprintf('%d', peak_idx), ...
'Position', [peak_idx, ax2.YLim(2)*0.16, 0], ...
... % ^^^ ax2 instead of ax1
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom');
text(ax2, 'String', sprintf('%d', peak_idx + DLY), ...
'Position', [peak_idx + DLY, ax2.YLim(2)*0.16, 0], ...
... % ^^^ ax2 instead of ax1
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom');
By the way, if you have store the axes handles in h1 and h2 already, using ax1=gca and ax2=gca is redundant. Prefer one of the two methods. Instead of:
h1 = subplot(2,1,1) ;
plot(M_lag,M_res) ;
title([ 'DLY = ' sprintf('%d',DLY) ]) ;
ax1 = gca ;
use
ax1 = subplot(2,1,1) ;
plot(M_lag,M_res) ;
title([ 'DLY = ' sprintf('%d',DLY) ]);
By the way, you are using ax2.YLim(2) to draw some elements, but the axes' limits are chosen automatically by Matlab. Therefore the location of these elements depends e.g. on the resolution of the monitor. Is this really wanted? You set the XTick values for the first plot, but the limits are not controlled, are they? Using ax1.Ylim to set elements in ax2 might make this worse.
  1 Kommentar
Wis
Wis am 12 Dez. 2018
Well, duh. Let's let that sink in. Score another for cut'n'paste programming.
Thanks for the help and suggestion/clarification on the handle.
Best regards to all.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by