How to assign a user input to a variable in matlab when using uicontrol 'string'?

uicontrol('Parent',d,...
'Style','edit',...
'Position',[20 330 210 60],...
'String','-4')
The -4 is just an example value, and can be changed by the user. I want to store this string value in a variable. Please help.
Thank you in advance.

 Réponse acceptée

Stephen23
Stephen23 le 13 Sep 2017
Modifié(e) : Stephen23 le 13 Sep 2017
hnd = uicontrol('Parent',d,... 'Style','edit',... 'Position',[20 330 210 60],... 'String','-4');
str = get(hnd,'String')
num = str2double(str)

4 commentaires

I ran this command, the value of num come to be -4. If i dont specify the string, num= NaN. I want the string to be manipulated according to the user input. It should not always give me -4. For example if the user wants to give -7 as input, so num= -7. Thank you for the help.
"It should not always give me -4"
It does not always give you -4. It gives you whatever the string in the edit box is. Have a look at str.
"For example if the user wants to give -7 as input, so num= -7"
It works for me, did you try it?
>> hnd = uicontrol('Style','edit'); % then I added '-7' by hand
>> get(hnd,'String')
ans = -7
>>
What do you expect to happen?
Note that by default MATLAB uicontrols do not trigger any action, so the simplest way of updating a variable based on any uicontrol value (e.g. the string) is to get that value just as I showed you in my answer. If you want the uicontrol to trigger some callback then you will have specify one of its callback methods:
or add some listener to your gui:
heading = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 310 210 100],...
'String','Enter the first value:');
txt = uicontrol('Parent',d,...
'Style','edit',...
'Position',[20 330 210 60],...
'String',' -4');
This -4 is just an example value, like default answer in the dialog box. I want to extract the user input value and assign it to a variable. The user can use any number, as it is a user interactive code.
"I want to extract the user input value and assign it to a variable. The user can use any number, as it is a user interactive code."
That is exactly what my code does. What you think this example shows?:
>> hnd = uicontrol('Style','edit'); % then I added '+3' by hand
>> get(hnd,'String')
ans = +3
Call get any time you want to check what the current string is. It is that simple. If you want a callback then add a callback (in which case you will also need get or the new dot syntax, whichever works for you).
Are you expecting MATLAB to magically update a variable any time the user changes the string in the edit box? This is possible, but it is quite a bit fiddlier than what I showed you in my answer, and it would be worth considering using nested functions. Have a look at my FEX submission iregexp for an example of how to do this:

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by