How to allow user to toggle errorbars on a plot using a check box?

조회 수: 1 (최근 30일)
Caitlin Taylor
Caitlin Taylor 2018년 12월 11일
편집: Caitlin Taylor 2018년 12월 11일
In default, the box is checked (=1) and the plots with error bars should be displayed....
% Energy Rise v Volume Flow Rate
fignum = 1;
tstFig(fignum) = figure(fignum);
set(tstFig(fignum), 'Position', [900 100 plot_width plot_height])
checkbox1 = uicontrol('Parent', tstFig(1),'style','checkbox','value',1,'Callback', @checkbox1_callback);
uicontrol(tstFig(fignum),'style', 'text','string', 'Toggle error bars?','position', [40,15,100,20]);
hold on;
for i = 1:nGrps
plot1 = errorbar(Qdot{i}, deltaE{i}, Err_deltaE{i}, Err_deltaE{i}, Err_Qdot{i}, Err_Qdot{i},...
'Color', cmap(i,:), ...
'LineStyle', linetype{i}, ...
'Marker', markertype{i}, ...
'DisplayName', strcat('Re-',num2str(i),' (', num2str(rpmGroups(i)*100),' RPM)' )...
);
hold on;
end
xlabel(sprintf('Volume Flow Rate, Q (m^3/s))'));
ylabel(sprintf('Energy Rise, \\DeltaE (m^2/s^2)'));
lgd = legend('show');
lgd.FontSize = lgdFontSize;
hold off;
grid on;
I would like to update the plot to display without the error bars if the box on the figure is unchecked:
%Just plotting w/o error bars
for i = 1:nGrps
plot2 = plot(Qdot{i}, deltaE{i},...
'Color', cmap(i,:), ...
'LineStyle', linetype{i}, ...
'Marker', markertype{i}, ...
'DisplayName', strcat('Re-',num2str(i),' (', num2str(rpmGroups(i)*100),' RPM)' )...
);
hold on;
end
xlabel(sprintf('Volume Flow Rate, Q (m^3/s))'));
ylabel(sprintf('Energy Rise, \\DeltaE (m^2/s^2)'));
lgd = legend('show');
lgd.FontSize = lgdFontSize;
hold off;
grid on;
When I try to make an if statement in the function below, it acts as if the variables are not defined, so I am not sure how to go about this.
function checkbox1_callback(hObject,eventdata)
h = get(hObject, 'Value');
end
I have tried an if statement in the main code containing the plot commands, but it doesnt seem to capture the value of the checkbox after it is unchecked. I can barely get by coding and I have major gaps in my understanding. I tried reading about using handles etc but I havent quite grasped it yet. Can anyone help me?
  댓글 수: 1
Jan
Jan 2018년 12월 11일
Please note that "When I try to make an if statement in the function below" is not clear. Prefer to post the code. Post the error message instead of the interpretation "it acts as if the variables are not defined".
"it doesnt seem to capture the value of the checkbox after it is unchecked" - of course, the code does not run again magically if you check the box. You need to use the callback for the actions.

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

채택된 답변

Jan
Jan 2018년 12월 11일
Create both plots at the same time and use the callback of the checkbox to togle the 'Visible' property:
H.WithErrorBar = gobjects(1, nGrps);
H.PlotOnly = gobjects(1, nGrps);
axes('NextPlot', 'add'); % Same as: hold on
for i = 1:nGrps
H.WithErrorBar(i) = errorbar(Qdot{i}, deltaE{i}, Err_deltaE{i}, Err_deltaE{i}, Err_Qdot{i}, Err_Qdot{i},...
'Color', cmap(i,:), ...
'LineStyle', linetype{i}, ...
'Marker', markertype{i}, ...
'DisplayName', strcat('Re-',num2str(i),' (', num2str(rpmGroups(i)*100),' RPM)' )...
);
H.PlotOnly(i) = plot(Qdot{i}, deltaE{i},...
'Color', cmap(i,:), ...
'LineStyle', linetype{i}, ...
'Marker', markertype{i}, ...
'DisplayName', strcat('Re-',num2str(i),' (', num2str(rpmGroups(i)*100),' RPM)' ), ...
'Visible', 'off');
end
checkbox1 = uicontrol('Parent', tstFig(1),'style','checkbox', ...
'value',1,'Callback', {@checkbox1_callback, H});
And in the callback:
function checkbox1_callback(hObject, eventdata, H)
value = get(hObject, 'Value');
if value == 1
set(H.WithErrorBar, 'Visible', 'off');
set(H.PlotOnly, 'Visible', 'on');
else
set(H.WithErrorBar, 'Visible', 'on');
set(H.PlotOnly, 'Visible', 'off');
end
end
  댓글 수: 1
Caitlin Taylor
Caitlin Taylor 2018년 12월 11일
편집: Caitlin Taylor 2018년 12월 11일
Thanks, this clarifies some things for me about how callbacks work.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by