Passing data from GUI to .m file

Hello.I made a code(.m file) that accepts some inputs from user and gives a file as an output.Now I decided to make it receive inputs by means of GUI.A GUI composed of six textboxes and one push button for submitting inputs and receiving output.I have problem that how I can pass variables(inputs)from GUI to .m file Here is code Thanks a lot

 Accepted Answer

Adam
Adam on 3 Sep 2014
Edited: Adam on 3 Sep 2014
Where you put, for example
k.name=get(hObject,'String')
in a callback, k there is just a local variable. As such it goes out of scope as soon as the callback completes.
What you need to do is attach this to the handles structure of the GUI, something more like:
handles.k.name = get( hObject, 'String' )
guidata( hObject, handles );
That last line is important to always include otherwise your changes to the handles structure will also be local to the callback function and lost after it completes.
Obviously you then also need to update your final callback equivalently:
ff=redesign( handles.k )
If you prefer you can ignore the text/edit box callbacks altogether and just collect up all the components from the UI under your pushbutton callback. That is often preferable if you do not need real time updating of the parameters in the underlying storage - i.e. if you are only going to use the values current at the time of clicking the pushbutton and don't care if the user changes their mind 27 times on what values they enter on the UI.

5 Comments

Thanks a lot Adam.Now it produces required result.But it generates some errors:
Output argument "ff" (and maybe others) not assigned during call to "C:\Users\Hossein\Documents\MATLAB\mainnacaredesign.m>mainnacaredesign".
Error in nacagui>submit_Callback (line 227) ff=mainnacaredesign(handles.k)
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in nacagui (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)nacagui('submit_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I don't know how I can correct them.Can you help me again? and A question:what does a Handle do?
Adam
Adam on 3 Sep 2014
Edited: Adam on 3 Sep 2014
It's difficult to help with those specific errors. There is no mainnacaredesign in the original code you attached.
The 'output argument not assigned' error tends to come from either the obvious source of you just not creating the output variable you then pass out or from some early return from your function before the variable gets created.
Using the debugging "stop on errors" option might help you, but you can probably start by just heading to the line mentioned in the error message. What is happening at line 227 of mainnacaredesign? Is it returning from a function and if so is the 'ff' variable in scope at the time it returns?
'handles' is just the structure used by guide to store all the gui components it creates, but is also one of the simplest ways to attach any data you like to a GUI by just doing as I showed above and adding your own fields to the struct. Be sure only ever to add to it though, never replace it with your own else you will lose all the GUI handles.
An individual handle is generally an id for a GUI component.
e.g.
hFig = figure;
'hFig' is now a handle (if you look at it in the variable editor it is in fact just a double) that allows you to identify the figure you just created at any point later on by e.g.
get( hFig, 'Position' )
to query its 'position' property.
Storing handles of the GUI objects you create is good practice rather than relying on things like gcf, gca or findobj to relocatate lost UI elements.
Dear adam.Thanks a lot!!mainnacaredesign is "redesign" in text file I send you.You said that ".... not creating the output variable" and I got the problem and fixed it.Again I want to thank you. Only one question:When user fill textboxes and presses "Submit" button,texts in textboxes don't disappear in other word I want to make it ready for another input from user.How can I do that?
Adam
Adam on 3 Sep 2014
Edited: Adam on 3 Sep 2014
Under your "Submit" button callback just add instructions to the effect of:
set( handles.text1, 'String', '' )
for all your text boxes, where 'text1' should be replaced by whatever tag you gave to your text boxes.
I would probably create a function called resetTextBoxes( handles ) or similar and do it in there, but that's just me being fussy. If you only have a 6 of them then just 6 statements in a row like the one above works.
Again Thanks a lot Adam

Sign in to comment.

More Answers (0)

Categories

Asked:

on 3 Sep 2014

Commented:

on 3 Sep 2014

Community Treasure Hunt

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

Start Hunting!