Read inputs from GUI into a second master script
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2017-8-23
编辑:Adam
2017-8-23
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.
采纳的回答
Adam
2017-8-23
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!)
0 个评论
更多回答(1 个)
Jan
2017-8-23
编辑:Jan
2017-8-23
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 个评论
Jan
2017-8-23
@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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!