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

1 vue (au cours des 30 derniers jours)
Caitlin Taylor
Caitlin Taylor le 11 Déc 2018
Modifié(e) : Caitlin Taylor le 11 Déc 2018
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 commentaire
Jan
Jan le 11 Déc 2018
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.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 11 Déc 2018
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 commentaire
Caitlin Taylor
Caitlin Taylor le 11 Déc 2018
Modifié(e) : Caitlin Taylor le 11 Déc 2018
Thanks, this clarifies some things for me about how callbacks work.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by