Azzera filtri
Azzera filtri

exotic piece of code

2 visualizzazioni (ultimi 30 giorni)
Wesley Ooms
Wesley Ooms il 14 Gen 2014
Commentato: AJ von Alt il 17 Gen 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 Commento
AJ von Alt
AJ von Alt il 17 Gen 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' )

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by