Help with SelectionChangeFcn (or how to define the callback function to trigger a specific response)

14 次查看(过去 30 天)
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.

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-24
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!

更多回答(1 个)

isdapi
isdapi 2014-11-24
Thanks Geoff for your invaluable help.
  1 个评论
asbin
asbin 2020-3-10
你好,我也在做类的功能,但是我关于 ‘SelectionChangedFcn’ 的内容总是报错,报错内容为:‘名称 'SelectionChangedFcn' 不是类 'com.mathworks.hg.peer.UITreePeer' 的实例的可访问属性。’可以帮帮我吗?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by