Read inputs from GUI into a second master script

1 view (last 30 days)
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
Ben Stuart
Ben Stuart on 23 Aug 2017
Edited: Ben Stuart on 23 Aug 2017
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

Adam
Adam on 23 Aug 2017
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.
Jan
Jan on 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.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!