How to assign a user input to a variable in matlab when using uicontrol 'string'?
8 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Stephen23
2017-9-13
编辑:Stephen23
2017-9-13
hnd = uicontrol('Parent',d,... 'Style','edit',... 'Position',[20 330 210 60],... 'String','-4');
str = get(hnd,'String')
num = str2double(str)
4 个评论
Stephen23
2017-9-13
"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:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!