Tuesday 15 January 2013

Passing Edit uicontrol string to callback of another uicontrol in Matlab -



Passing Edit uicontrol string to callback of another uicontrol in Matlab -

i wrote code in matlab:

function[] = gui_function() window.value1 = uicontrol('style', 'edit', ... 'string', '5', ... 'callback', @is_number); window.computebutton = uicontrol('style', 'push', ... 'callback', {@test_script, str2double(get(window.value1, 'string'))}); end function[] = test_script(varargin) value1 = varargin{3}; end

i want pass text edit uicontrol button's callback. when following, value passed old value set when declaring uicontrol. ie. run gui , have value of 5 in edit. overwrite 20, after pushing button, value beingness passed still 5

what wrong in approach? how can done differently? give thanks in advance!

in opinion, best alternative when working guis utilize handles construction of gui, in every uicontrols stored along associated properties, in add-on (that's cool part) whatever want store in it, variables instance.

so modified code bit create utilize of handles structure. i'm not exclusively clear want, in illustration pushbutton used update content of sec edit box content of 1st edit box. that's basic, should help sense of handles , handles structure. if unclear please allow me know!

function gui_function() screensize = get(0,'screensize'); handles.figure = figure('position',[screensize(3)/2,screensize(4)/2,400,285]); handles.edit1 = uicontrol('style', 'edit','position',[100 150 75 50], ... 'string', '5'); handles.edit2 = uicontrol('style', 'edit','position',[100 80 75 50], ... 'string', 'update me'); handles.computebutton = uicontrol('style', 'push','position',[200 100 75 75],'string','pushme', ... 'callback', @pushbuttoncallback); guidata(handles.figure, handles); %// save handles guidata. it's accessible form everywhere in gui. function pushbuttoncallback(handles,~) handles=guidata(gcf); %// retrieve handles associated figure. textinbox1 = get(handles.edit1,'string'); set(handles.edit2,'string',textinbox1); %// update 2nd edit box content of first. %// whatever want... guidata(handles.figure, handles); %// don't forget update handles construction

you customize gui adding function callback (test_script) in same way implemented pushbuttoncallback. hope understood wanted :)

matlab callback

No comments:

Post a Comment