How can I create a checkbox in a new figure and control it from a GUI?
23 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
Walter Roberson
2016 年 10 月 15 日
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
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.
0 件のコメント
その他の回答 (1 件)
Venkat Ta
2019 年 7 月 27 日
1. How I get checkbox values without using any push-button call back?
2. How I get default values without callback function
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!