How can I create a checkbox in a new figure and control it from a GUI?

23 Ansichten (letzte 30 Tage)
Hi,
I have a GUI that, by pushing a button, creates a new figure with a set of checkboxes plus a control button. The problem is that I can create the figure with all I need,however I don't know how to control the value of the checkboxes via the control button I've created. As an example, let us assume to create a single checkbox and a control button in a new figure (by pushing a button in another GUI). When I press the just created control button, I would like to set the value of the checkbot to 1. However, when I try to create a callback function for the pushbutton I always get error (seems that the handles doesn't exist).
From a previous question on the furum https://www.mathworks.com/matlabcentral/answers/13351-dialog-with-checkboxes-in-gui I have got the code to create the figure with the checkboxes and the control button, but still I cannot make the control button to control the checkboxes. I report the code in the link above:
h.f = figure('units','pixels','position',[200,200,150,50],...
'toolbar','none','menu','none');
% Create yes/no checkboxes
h.c(1) = uicontrol('style','checkbox','units','pixels',...
'position',[10,30,50,15],'string','yes');
h.c(2) = uicontrol('style','checkbox','units','pixels',...
'position',[90,30,50,15],'string','no');
% Create OK pushbutton
h.p = uicontrol('style','pushbutton','units','pixels',...
'position',[40,5,70,20],'string','OK',...
'callback',@p_call);
% Pushbutton callback
function p_call(varargin)
vals = get(h.c,'Value');
checked = find([vals{:}]);
if isempty(checked)
checked = 'none';
end
disp(checked)
end
Basically, I cannot make the callback fucntion p_call to handle the "h.c2 checkboxes...
Any help?
Thanks a lot

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Okt. 2016
One way:
h.f = figure('units','pixels','position',[200,200,150,50],...
'toolbar','none','menu','none');
% Create yes/no checkboxes
h.c(1) = uicontrol('style','checkbox','units','pixels',...
'position',[10,30,50,15],'string','yes');
h.c(2) = uicontrol('style','checkbox','units','pixels',...
'position',[90,30,50,15],'string','no');
% Create OK pushbutton
h.p = uicontrol('style','pushbutton','units','pixels',...
'position',[40,5,70,20],'string','OK');
set(h.p, 'callback', @(src, event) p_call(src, event, h));
with
% Pushbutton callback
function p_call(src, event, h)
vals = get(h.c,'Value');
checked = find([vals{:}]);
if isempty(checked)
checked = 'none';
end
disp(checked)
end
The h that will be passed to the callback will be a copy of the h as it existed at the time the callback was created. That is why I split it up into creation of a control and setting the callback -- in that way, h.p is populated before a copy of h is taken for the callback. If you combine them the way you had done, the h that would be passed in would be the version from before h.p had been assigned to.

Weitere Antworten (1)

Venkat Ta
Venkat Ta am 27 Jul. 2019
1. How I get checkbox values without using any push-button call back?
2. How I get default values without callback function

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by