Help with SelectionChangeFcn (or how to define the callback function to trigger a specific response)
14 views (last 30 days)
Show older comments
Hi, I'm not a great expert with uicontrols and more specifically with uibuttongroups and I need some help.
The thing is I've defined a buttongroup with three radiobuttons linked to it:
dpb = uibuttongroup('FontSize',12,'Position',[.015 .65 .17 .28]);
u0 = uicontrol(dpb,'Style','radiobutton','String','P-130',...
'Position',[30 110 100 30],'BackgroundColor',[.8,.8,.8],'Tag','1');
u1 = uicontrol(dpb,'Style','radiobutton','String','FAST',...
'Position',[30 65 100 30],'BackgroundColor',[.8,.8,.8],'Tag','2');
u2 = uicontrol(dpb,'Style','radiobutton','String','SLOW',...
'Position',[30 20 100 30],'BackgroundColor',[.8,.8,.8],'Tag','3');
Then I added some properties to the uibuttongroup:
set(dpb,'SelectedObject',[]); % No selection
set(dpb,'SelectionChangeFcn',@dpbcbk); %Deceleration profile's callback
In other part of my script I've created a edit uicontrol (pdp is a uipanel which is parented to) that I want to be updated with a given value depending on the user selection of the aforementioned uibuttongroup:
edt4 = uicontrol(pdp,'Style','edit','Position',[330 200 80 20]);
My problem is to define properly the callback function to get this done. I've tried it (although to no avail) with the following callback:
function dpbcbk(source,eventdata)
switch get(eventdata.NewValue,'tag')
case '1'
set(edt4,'String','130');
case '2'
set(edt4,'String','150');
case '3'
set(edt4,'String','110');
end
This are the error messages I get:
Undefined function or variable 'edt4'.
Error in dpbcbk (line 8)
set(edt4,'String','110');
Error in hgfeval (line 63)
feval(fcn{1},varargin{:},fcn{2:end});
Error in uitools.uibuttongroup/childAddedCbk>manageButtons (line 79)
hgfeval(cbk, source, evdata);
I know that there is something that I'm missing, but I don't know much about how to work with callbacks. Any input is welcome.
0 Comments
Accepted Answer
Geoff Hayes
on 24 Nov 2014
Ismael - you can try nesting your callback within your function that creates the GUI. A benefit of nested functions is that they have access to the local variables within the "main" function. Try the following
function build_GUI
% create the figure
hFig = figure('Position',[0 0 968 542]);
% create the button group
dpb = uibuttongroup('FontSize',12,'Position',[.015 .65 .17 .28]);
% add the radio buttons to the button group
u0 = uicontrol(dpb,'Style','radiobutton','String','P-130',...
'Position',[30 110 100 30],'BackgroundColor',[.8,.8,.8],'Tag','1');
u1 = uicontrol(dpb,'Style','radiobutton','String','FAST',...
'Position',[30 65 100 30],'BackgroundColor',[.8,.8,.8],'Tag','2');
u2 = uicontrol(dpb,'Style','radiobutton','String','SLOW',...
'Position',[30 20 100 30],'BackgroundColor',[.8,.8,.8],'Tag','3');
% set the callbacks
set(dpb,'SelectedObject',[]); % No selection
set(dpb,'SelectionChangeFcn',@dpbcbk); %Deceleration profile's callback
% do other stuff
% create the edit box (note hFig being used instead of your pdp)
edt4 = uicontrol(hFig,'Style','edit','Position',[330 200 80 20]);
% define the (nested) button group callback
function dpbcbk(source,eventdata)
switch get(eventdata.NewValue,'tag')
case '1'
set(edt4,'String','130');
case '2'
set(edt4,'String','150');
case '3'
set(edt4,'String','110');
end
end % end for dpbcbk
end % end for build_GUI
Now, the SelectionChangeFcn callback, which hasn't changed, has access to the edt4 handle, a local variable of build_GUI, and can update this control.
Try the above and see what happens!
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!