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

1 次查看(过去 30 天)
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

更多回答(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