exotic piece of code
13 ビュー (過去 30 日間)
古いコメントを表示
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
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 Center および File Exchange で Migrate GUIDE Apps についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!