Read inputs from GUI into a second master script
Ältere Kommentare anzeigen
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 Kommentare
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.
Ben Stuart
am 23 Aug. 2017
Bearbeitet: Ben Stuart
am 23 Aug. 2017
Adam
am 23 Aug. 2017
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'.
Ben Stuart
am 23 Aug. 2017
Adam
am 23 Aug. 2017
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.
Ben Stuart
am 23 Aug. 2017
Bearbeitet: Ben Stuart
am 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.
Akzeptierte Antwort
Weitere Antworten (1)
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 Kommentare
Jan
am 23 Aug. 2017
@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.
Kategorien
Mehr zu Structures finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!