How to update titles and text annotations of subplots located on the same uitab?

조회 수: 6 (최근 30일)
theblueeyeswhitedragon
theblueeyeswhitedragon 2018년 6월 20일
답변: Naga 2024년 10월 23일 4:56
% TAB 1
S.tabH(1) = uitab(S.histoTab, 'Title', 'Error X');
S.P1_histo{1} = subplot(2,2,[1 3], 'Parent', S.tabH(1));
s2.tab1sp1 = hggroup;
S.P1_histo{2} = subplot(2,2,[2 4], 'Parent', S.tabH(1));
s2.tab1sp2 = hggroup;
% TAB 2
S.tabH(2) = uitab(S.histoTab, 'Title', 'Error Y');
S.P2_histo{1} = subplot(2,2,[1 3], 'Parent', S.tabH(2));
title('On Fly');
s2.tab2sp1 = hggroup;
S.P2_histo{2} = subplot(2,2,[2,4], 'Parent', S.tabH(2));
title('Static');
s2.tab2sp2 = hggroup;
%%In the Callback Function
handles = guidata(source);
currentTab = find(source.SelectedTab == S.tabH);
switch currentTab
case 1
if ~isempty(handles.R_Site_X_Static_NM_DRF)
histogram(handles.R_Site_X_Static_NM_DRF, 'parent',s2.tab1sp2);
ThreeSig_X_S = 3 * std(handles.R_Site_X_Static_NM_DRF);
stringH1 = strcat({'Static, '},{'3sig: '}, {num2str(ThreeSig_X_S)});
title(S.P1_histo{1}, 'Static');
text(S.P1_histo{1},0, 200, stringH1);
end
if ~isempty(handles.R_Site_X_OnFly_NM_DRF)
histogram(handles.R_Site_X_OnFly_NM_DRF, 'parent', s2.tab1sp1);
ThreeSig_X_OF = round(3 * std(handles.R_Site_X_OnFly_NM_DRF),3);
stringH2 = strcat({'3sig: '}, {num2str(ThreeSig_X_OF)});
title(S.P1_histo{2}, 'On Fly');
text(S.P1_histo{2},0.2, 130, stringH2, 'FontWeight', 'Bold');
end
case 2
...
I am getting the title and text annotation only for the second subplot of tab 1. When debugging the code line by line, I get the title and text for the first subplot too, but it later disappears.

답변 (1개)

Naga
Naga 2024년 10월 23일 4:56
The issue you're facing with the title and text annotations disappearing could be related to the use of hggroup as the parent for your histograms. When you update a plot with a new plot command (like histogram), it usually clears the axes unless you specify otherwise. Here are a few suggestions to fix this issue:
  1. Before plotting the histogram, explicitly set the current axes using the axes() function to ensure that the plots and annotations are directed to the correct subplot.Directly call the histogram function to plot the data on the specified axes.
  2. Use the title() and text() functions to add titles and annotations to the plots. Construct annotation strings using strcat() to include statistical information.
  3. Ensure that the plots and annotations are not inadvertently cleared by setting the axes context appropriately.
This approach ensures that each subplot retains its title and annotations after plotting. Please find the updated code:
% TAB 1
S.tabH(1) = uitab(S.histoTab, 'Title', 'Error X');
S.P1_histo{1} = subplot(2,2,[1 3], 'Parent', S.tabH(1));
S.P1_histo{2} = subplot(2,2,[2 4], 'Parent', S.tabH(1));
% TAB 2
S.tabH(2) = uitab(S.histoTab, 'Title', 'Error Y');
S.P2_histo{1} = subplot(2,2,[1 3], 'Parent', S.tabH(2));
title(S.P2_histo{1}, 'On Fly');
S.P2_histo{2} = subplot(2,2,[2 4], 'Parent', S.tabH(2));
title(S.P2_histo{2}, 'Static');
% Callback Function
handles = guidata(source);
currentTab = find(source.SelectedTab == S.tabH);
switch currentTab
case 1
if ~isempty(handles.R_Site_X_Static_NM_DRF)
axes(S.P1_histo{1});
histogram(handles.R_Site_X_Static_NM_DRF);
title('Static');
text(0, 200, strcat('Static, 3sig: ', num2str(3 * std(handles.R_Site_X_Static_NM_DRF))));
end
if ~isempty(handles.R_Site_X_OnFly_NM_DRF)
axes(S.P1_histo{2});
histogram(handles.R_Site_X_OnFly_NM_DRF);
title('On Fly');
text(0.2, 130, strcat('3sig: ', num2str(round(3 * std(handles.R_Site_X_OnFly_NM_DRF), 3))), 'FontWeight', 'Bold');
end
case 2
% Additional code for case 2
end

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by