Is this a switchyarding case?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a figure with 4 checkboxes. I want to fill a 1x4 array with values of 1 if the checkbox is selected. What I've written below somewhat works, but only allows one value of chkbxvals to change and won't keep any previous values. All four checkboxes use the same callback.
So for example, if the user clicks on all four boxes, I want chkbxvals = [1 1 1 1]. If the user unchecks the first box, chkbxvals = [0 1 1 1]. If none are selected, then chkbxvals = [0 0 0 0]; and so on...
I know the long way to do this is to create 16 loops to cover all possible situations, but I feel as though there may be a more compact way to do it.
function input_chkbx_Callback(gcf,eventdata,handles,chkbxval)
chkbxvals = [0 0 0 0];
switch chkbxval
case 1
chkbxvals(1) = 1;
case 2
chkbxvals(2) = 1;
case 3
chkbxvals(3) = 1;
case 4
chkbxvals(4) = 1;
end
Thanks!
0 Kommentare
Akzeptierte Antwort
Matt Fig
am 14 Apr. 2011
Here is an example.
function [] = Check_examp()
% Print to screen the users checkbox choices.
S.fh = figure('units','pixels',...
'position',[500 500 200 220],...
'menubar','none',...
'name','Check_boxes',...
'numbertitle','off',...
'resize','off');
S.ch(1) = uicontrol('style','check',...
'unit','pix',...
'position',[10 60 180 20],...
'fontsize',14,...
'string','checkbox1');
S.ch(2) = uicontrol('style','check',...
'unit','pix',...
'position',[10 100 180 20],...
'fontsize',14,...
'string','checkbox2');
S.ch(3) = uicontrol('style','check',...
'unit','pix',...
'position',[10 140 180 20],...
'fontsize',14,...
'string','checkbox3');
S.ch(4) = uicontrol('style','check',...
'unit','pix',...
'position',[10 180 180 20],...
'fontsize',14,...
'string','checkbox4');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Get Boxes',...
'callback',{@pb_call,S});
function [] = pb_call(varargin)
% Callback for pushbutton, gets checkbox choices.
S = varargin{3}; % Get the structure.
V = cell2mat(get(S.ch(4:-1:1),'value')) % Get the users choice.
.
.
.
Note that if you are using GUIDE, you should modify the code in the callback by getting the handles to the checkboxes into a single vector when querying their value property. Something like:
S =[handles.ch1,handles.ch2,handles.ch3,handles.ch4];
V = cell2mat(get(S,'value'))
Also, note that I put the code in the callback to a pushbutton, but you can put it anywhere.
13 Kommentare
Matt Fig
am 14 Apr. 2011
Oh, I see what happened. S does not have a field called pb when the callback is created! Use this instead:
set(gcbo,'callback',{@pb_call,S})
Or go back and initialize S.pb, then in a separate line assign the callback with:
set(S.pb,'callback',{@pb_call,S})
Weitere Antworten (1)
Walter Roberson
am 14 Apr. 2011
What is the Callback property set to for these checkboxes? Callbacks do not take 4 inputs unless the Callback property has been set up specially, and the typical way of doing so would end up with the 4th argument always being the same as the value was when the callback property was set.
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!