passing uicontrol objects to uicontrol callbacks
Mostrar comentarios más antiguos
I have three sliders that are in the same position, and based on a radio button selection, I'd like to make one of the three appear at one time.
I've tried to do this by making only one visible at any point in time. I've tried to accomplish this by passing the slider uicomponents to the respective callback functions of the radio buttons, so that i might make one visible and the other two invisible.
Essentially,
%%%%%Make slider objects at same position%%%%%%
p=uipanel;
sliderterminal = uicontrol(p,'Style','slider');
sliderinitial = uicontrol(p,'Style','slider');
sliderthreshold = uicontrol(p,'Style','slider');
%%%%%controlling radio buttons%%%%%%
bg = uibuttonggroup(p);
rinitial = uicontrol(bg,'Style','radiobutton','Callback',@initialcutoff)
rterminal = uicontrol(bg,'Style','radiobutton','Callback',@terminalcutoff)
rthreshold = uicontrol(bg,'Style','radiobutton','Callback',@thresholdindex)
%%%%%%Callback Functions%%%%%
function initialcutoff(sliderthreshold,sliderinitial,sliderterminal)
sliderterminal.Visible='off';
sliderthreshold.Visible='off';
sliderinitial.Visible='on';
end
function terminalcutoff(sliderthreshold,sliderinitial,sliderterminal)
sliderinitial.Visible='off';
sliderthreshold.Visible='off';
sliderterminal.Visible='on';
end
function thresholdindex(sliderthreshold,sliderinitial,sliderterminal)
sliderinitial.Visible='off';
sliderterminal.Visible='off';
sliderthreshold.Visible='on';
end
When the callback functions are accessed the desired uicontrol objects are not passed in from the base workspace. If anybody could help me with this issue I would greatly appreciate insight. Please let me know if I'm going about this all wrong.
PS - I didn't add the position information for the uicomponents as i figured added a lot of extraneous information isn't too good of an idea.
Respuesta aceptada
Más respuestas (2)
Shantanu Gontia
el 11 de Jul. de 2018
There are two ways you can do this
1. Make the sliderterminal, sliderinitial, sliderthreshold variables global and use them directly in the callbacks without a need of passing them as arguments.
2. In the callback property of the radio buttons, instead of passing the functions initialcutoff, terminalcutoff, thresholdindex, create anonymous functions that call these functions with the required arguments. For instance,
rinitial = uicontrol(bg,'Style','radiobutton','Callback', @(varargin) initialcutoff(sliderthreshold, sliderinitial, sliderterminal));
You can code similarly for the other buttons as well.
2 comentarios
Jan
el 11 de Jul. de 2018
Global variables are a bad idea in every case.
Steve Slana
el 11 de Jul. de 2018
You can use one callback for all 3 radio buttons:
handles.p = uipanel;
handles.slider(1) = uicontrol(p, 'Style', 'slider');
handles.slider(2) = uicontrol(p, 'Style', 'slider');
handles.slider(3) = uicontrol(p, 'Style', 'slider');
handles.bg = uibuttonggroup(p);
uicontrol(bg, 'Style', 'radiobutton', 'Callback', {@initial, handles, 1})
uicontrol(bg, 'Style', 'radiobutton', 'Callback', {@initial, handles, 2})
uicontrol(bg, 'Style', 'radiobutton', 'Callback', {@initial, handles, 3})
function initial(RadioH, EventData, handles, Index)
set(handles.slider, 'Visible', 'off');
set(handles.slider(Index), 'Visible', 'on');
end
1 comentario
Steve Slana
el 11 de Jul. de 2018
Editada: Steve Slana
el 11 de Jul. de 2018
Categorías
Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!