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?

 採用された回答

Kevin Phung
Kevin Phung 2019 年 3 月 14 日
編集済み: Kevin Phung 2019 年 3 月 14 日

0 投票

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

4 件のコメント

NURUL FARINA BINTI MAZLAN
NURUL FARINA BINTI MAZLAN 2019 年 3 月 14 日
Will the disp(hObj.String) keeps the value of the input and can I name it under a variable which the variable will be used in another function?
NURUL FARINA BINTI MAZLAN
NURUL FARINA BINTI MAZLAN 2019 年 3 月 14 日
edit_speed = uicontrol('Style','edit','Position',[550,375,100,25],...
'Callback',{@newValueFromString});
function [ newValue, validNumber, inBounds ] = newValueFromString( ...
N, textString)
textString = get(edit_speed,'String');
[ inputNumber, validNumber ] = str2num( textString );
end
Is it possible for me to get the value of the input from the edit box and use it in other function using the code above?
Kevin Phung
Kevin Phung 2019 年 3 月 14 日
'Is it possible for me to get the value of the input from the edit box and use it in other function using the code above?'
yes, i mentioned that in my answer.
You can also store data in the 'UserData' property of your graphic objects.
so:
function [ newValue, validNumber, inBounds ] = newValueFromString( ...
N, textString)
textString = get(edit_speed,'String');
[ inputNumber, validNumber ] = str2num( textString );
edit_speed.UserData = textString
end
NURUL FARINA BINTI MAZLAN
NURUL FARINA BINTI MAZLAN 2019 年 3 月 14 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by