Numbers in a editbox to the workspace

1 次查看(过去 30 天)
Hi
I need to make a simple gui that has a editbox and a pushbutton. What I need help with doing, is that when I put a number inside the editbox and presses the pushbutton, then I need the number to be saved on the workspace. fx:
Editbox [ 2 ] [Push button] <-- after pressing that
I want it to say on the workspace: a = 2
And if I change Editbox [ 2 ] to Editbox [ 7 ], then I want the workspace to be updated to say: a = 7.
How do I do that? im quite lost here..

采纳的回答

Sean de Wolski
Sean de Wolski 2012-6-28
Here is a small example:
figure('units','norm');
hE = uicontrol('style','edit','units','norm','position',[.4 .4 .2 .2]);
uicontrol('style','push','units','norm','position',[.1 .1 .1 .1],'string',...
'Save2Workspace','callback',@(src,evt)assignin('base','X',str2double(get(hE,'string'))));
  3 个评论
Sean de Wolski
Sean de Wolski 2012-6-28
So here's a lesson in good programmign practice:
The edit callback should check to make sure the input is indeed a number.
Something like this:
val = str2double(get(hObject,'string'));
if isnan(val)
errordlg('You entered non numeric input');
end
Then the push button callback would be the equivalent of what I have above translated to guide
assignin('base','X',str2double(get(handles.edit1,'string'))));
Note: there may be typos this isn't tested.
Mikkel
Mikkel 2012-6-28
You are a darling! its just what I needed :) if you are a guru in matlab gui maybe you could help me with my other problem. Have another quition, and some one tryed to anwser it, but it hasent been solved yet. :/

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by