Filter löschen
Filter löschen

Numbers in a editbox to the workspace

1 Ansicht (letzte 30 Tage)
Mikkel
Mikkel am 28 Jun. 2012
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..

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 28 Jun. 2012
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 Kommentare
Sean de Wolski
Sean de Wolski am 28 Jun. 2012
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 am 28 Jun. 2012
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. :/

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by