MATLAB: Get value from uicontrol editbox

#getvaluefrom_uicontrol_editbox

Hi,
I have setup a uicontrol edit box into my code and expected to receive certain value from user. However, what kind of syntax can I use to get the value from the edit box to be used in other function?

Best Answer

You either set a callback function for that edit box, that will be triggered whenever the user hits the enter key,
edit = uicontrol('style','edit','Callback',@user_edit)
function user_edit(hObj,event) %these parameters are by default.
% hObj is the handle to your button.
disp(hObj.String) %this will display the current string
end
or, if you want to retrieve the string from beyond the callback function, assign a tag to your edit box so that you can point back to it:
set(edit,'Tag','edit1')
%or
edit.Tag = 'edit1';
function someotherfunction()
my_edit = findobj(gcf,'Tag',edit1);
disp(my_edit.String)
end