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

callbackguihandles

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?

Best Answer

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
Related Question