exotic piece of code

2 views (last 30 days)
Wesley Ooms
Wesley Ooms on 14 Jan 2014
Commented: AJ von Alt on 17 Jan 2014
I have a quite exotic piece of code that works fine, but i wonder if it cannot be done easier.
The code is part of a function and therefore ed and sl are only available in the function workspace. I cannot use gco or gcbo since ed changes sl and vice versa. Therefor i use evalin. I could use a function callback but a function is not allowed in a script so then i cannot easily change the function into a script. If i write a separate function, the code would not be a single file anymore which is also not desired.
sl=uicontrol('style','slider','Min',0,'Max',1e5,'Value',sc,'Position',[10,10,60,30],'Callback','evalin(''caller'',''x=get(sl,''''value'''');set(ed,''''string'''',x,''''userdata'''',x)'')');
ed=uicontrol('Style','edit','backgroundColor','w','FontSize',12,'String',sc,'Position',[80,10,80,30],'Callback', ...
['evalin(''caller'',''val=get(ed,''''string'''');x=str2double(val);if isnan(x)||x<get(sl,''''min'''')||x>get(sl,''''max'''');s' ...
'et(ed,''''string'''',get(ed,''''userdata''''));return;end;set(sl,''''value'''',str2double(val));set(ed,''''userdata'''',val);'')']);
  1 Comment
AJ von Alt
AJ von Alt on 17 Jan 2014
It would be conceivable to do this with anonymous functions in a script, but the resulting code would be very nasty and difficult to maintain.
The best solution would be to write two callback functions and accept that you may need more than one file if you make this one a script.
In terms of working with the uicontrol handles, it is true that sl and ed are local to the function, but the actual handles are associated with the figure containing the controls. You can access these handles through:
get( gcf , 'children' )
Another option is to use getappdata and |setappdata|to access these handles.
These commands can be used to store the handles:
setappdata(gcf , 'slHandle' , sl )
setappdata(gcf , 'edHandle' , ed )
These commands can be used to retrieve the handles:
sl = getappdata( gcf , 'slHandle' )
ed = getaoodata( gcf , 'edHandle' )

Sign in to comment.

Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!