exotic piece of code

1 次查看(过去 30 天)
Wesley Ooms
Wesley Ooms 2014-1-14
评论: AJ von Alt 2014-1-17
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 个评论
AJ von Alt
AJ von Alt 2014-1-17
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' )

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by