Info
This question is closed. Reopen it to edit or answer.
I’m using uncontrol check box , by command in a script, I have associated a function with check box, how can I get the handle of that function out
1 view (last 30 days)
Show older comments
uicontrol('style', 'checkbox', 'string', 'plz tick', 'callback',{@dt,xm};
function h = dt(hObject,xm,~)
h = line([xm xm] , [-1 1])
end
%%% I want to have the handle of line h , in my script as i click on check box
5 Comments
Rik
on 6 Jan 2019
Edited: Rik
on 6 Jan 2019
Once the callback function returns, its workspace is cleared. The ouputs of a callback don't go anywhere and are ignored. So if you want to get something out, you will have to save it somewhere. Then you could retrieve it back from that location in the function where you need to use that handle. You can save the handle using the UserData property, the guiddata function, or setappdata/getappdata. You could also edit the Tag property of the line object, so you can search for it.
What did you try, and why do you think it failed?
Answers (1)
Michael C.
on 3 Jan 2019
If I were doing something like this, I woudl do one of two things:
% Add this line inside "dt" function
set(hObject,'UserData',h)
% Use this to pull the line handle back into scope when you need it
% (this assumes that there is only one check box with the string 'plz tick')
hLine = get(findobj('String','plz tick'),'UserData')
or make a change to your "line" call
h = line([xm xm] , [-1 1],'Tag','myLine');
Then use
% Use this to pull the line handle back into scope
hLine = findobj('Tag','myLine');
In the second option, you might need to be a little careful depending on how the 'NextPlot' property is set on the axes you are plotting to as you could get into the situation where you end up with vector of handles to a lot of lines.
It looks like your callback might have the last two inputs swapped as well. I think it should be "function h = dt(hObject,~,xm,)"
1 Comment
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!