Read inputs from GUI into a second master script

Hi,
I am trying to read inputs from a basic GUI made in GUIDE, save them as doubles, then use them in subsequent analysis. I can't seem to use the defined inputs in my main file with error: Undefined function or variable 'dt'.
Fairly new to MatLAB and my first GUI. My code is currently:
% --- Executes on button press in RUN.
function RUN_Callback(hObject, eventdata, handles)
% hObject handle to RUN (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Save Handles
guidata(hObject,handles);
% Save Inputs
service_period = str2double(get(handles.Service_Period, 'String'))
no_of_runs = str2double(get(handles.no_of_runs,'String'))
dt = str2double(get(handles.dt, 'String'))
% Change run button to running
set(handles.RUN, 'String', 'Running')
% Run analysis
runBen3
Any ideas what I'm doing wrong?
Thanks
Ben

7 Comments

What is the full error message? Which line of code is it pointing to? There is no '*' anywhere in the code you have shown us.
There may be plenty of them in
runBen3
though, whatever that is.
Full error message:
Undefined function or variable 'dt'.
Error in runBen3 (line 10)
dt
Error in GUI>RUN_Callback (line 181)
runBen3
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)GUI('RUN_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating DestroyedObject Callback
dt is the first variable it calls as part of runBen3. runBen3 is my main script which reads data from excel, performs a Monte Carlo Simulation then spits out a series of graphs and tables with results.
Ok, at least the error message makes sense now. I've never seen an error message claiming that '*' is an undefined function or variable before!
If runBen3 is truly a script then dt ought to be defined, but if it is in fact a function instead then dt would have to be passed in. It would help if you could show the first 10 lines of runBen3.
In general running a script from a GUI is not a great idea. It is far better to turn it into a function and pass the variables in. Nevertheless, ignoring thoughts of good coding practice, the workspace of your script should be that in which it is run which, from what you have shown so far, should include 'dt'.
first lines of runBen3 as follows:
tic
clc
clear
close all
msgbox('Close the used excel file before running or script will fail')
%%%TEST %%%
dt
service_period
no_of_runs
%%%%%%%%%%
readgroups
Where readgroups is a separate script using a series of xlsreads to extract data. The test section is only included so the program fails before the lengthy readgroups. dt is first used properly on line 31.
It doesn't seem to define dt on my workspace unless I use de-bugging to walk it through line by line, and even then it doesn't seem to be able to then use it. I will have a go at saving runBen3 as a function and loading rather than saving as Jan has suggested.
Ok, well this is obvious what is wrong and is an error so many people make.
clc
clear
close all
may seem good, but you really need to think what you are doing.
clear
in particular clears all variables, including the ones you just created in your GUI. Get rid of this and your error will go away.
haha gees that is particulary idiotic of me. I may have to delete this question
Adam
Adam on 23 Aug 2017
Edited: Adam on 23 Aug 2017
The question is definitely useful for others. This is surprisingly common so useful for future reference. Deleting questions when you get an answer and people have put time into helping you find the answer is generally frowned upon since that time spent can also help others with the same problem.

Sign in to comment.

 Accepted Answer

To put the summary of all this in an answer.
Think what you are doing before just always putting any or all of the following in a script:
clear
clear all
close all
etc.
If you are expecting to have variables defined in a GUI then clearing them all at the start of your script is not very useful.
If you used a function instead of a script you wouldn't feel the need to do this at all as each function has its own workspace and doesn't retain all the detritus from whatever workspace it was called from (nor does it pollute said workspace with its own variables once it has completed!)

More Answers (1)

Jan
Jan on 23 Aug 2017
Edited: Jan on 23 Aug 2017
Do not start the function with saving the handles, but with loading them:
% No:
% % Save Handles
% guidata(hObject,handles);
% [EDITED, even this can be omitted: (thanks Adam)]
% handles = guidata(hObject);
Prefer to move the processing to a function instead of a script. This improves the stability of the code and is less prone to bugs. Then provide the data as inputs:
runBen3(service_period, no_of_runs, dt)
and a corresponding definition of the function.

2 Comments

Adam
Adam on 23 Aug 2017
Edited: Adam on 23 Aug 2017
As a GUIDE callback, loading the handles is superfluous too as they are passed in and, unlike when done in a user callback, they will be the up to date handles just the same as you would get from calling guidata.
@Adam: It is nice to read this, thanks. You are right: the handles struct from the inputs is updated automatically by GUIDE. I avoid working with GUIDE and hesitated to check this detail. Now I've checked this and it is true for R2016b and 2009a also.

Sign in to comment.

Categories

Asked:

on 23 Aug 2017

Edited:

on 23 Aug 2017

Community Treasure Hunt

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

Start Hunting!