필터 지우기
필터 지우기

exotic piece of code

조회 수: 2 (최근 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