MATLAB Answers

GUI output of structure generated by running a script

4 views (last 30 days)
Joel Affolter
Joel Affolter on 30 Sep 2019
Commented: Rik on 30 Sep 2019
Good Morning,
I have written two scripts which function on their own and which import data from Excel files and store it in a structure. Now I want to make my script more user friendly by implementing them into a GUI. If this works, I want to modify the second script, so that the values of some variables can be set via the GUI and does not need to be modified in the script. Sadly, I already fail in the first part.
Because the output I want to generate does not exist from the beginning, but is only generated after the script is run upon a pressing of a pushbutton, I changed the opening function and the closing function.
function simple_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% UIWAIT makes gui1 wait for user response (see UIRESUME)
set( hObject, 'WindowStyle', 'modal' );
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
% The GUI is no longer waiting, just close it
The code for the pushbutton is the following, where
is one of the two scripts which should be run to import and modify the experimental data. All data is then stored in the structure "AutarkyData"
function pushbutton1(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Display surf plot of the currently selected data
run AutarkyDataMain.m
handles.AutarkyData = AutarkyData;
According to my knowledge, upon pressing the pushbutton1 the following happens:
1. The AutarkyDataMain.m script is run
2. The structure "AutarkyData" is assigned to the handle-structure under the fieldname "AutarkyData"
3. The handly structure is globally updated
If open the GUI and press the button, the script is run as it should. But as soon as I close the GUI to receive the ouput, I get the following error message:
"Reference to non-existent field 'AutarkyData'."
I played a bit with the code and think I can narrow the source of the error.
The second script does also modify the data, but does so by assuming some "standard values" and varying other, while the first one (with which the abovehand mentionned error occurs) does import the values via a UI which alllows to select the Excel files which shall be imported.
[filenamecell, filepath] = uigetfile('*.xls;*.xlsx', 'Select Exel data file','MultiSelect', 'on'); %open UI to select the path
when running the code from above with the second script, the output of the AutarkyData structure from the GUI is succesful. This leads me to believe that the problem may arise because of the "UI within the GUI", but my knowledge in Matlab is limited, so this is pure speculation.
Does anybody know where the mistake lies and has an alternative code which will work for me?
Thanks in advance


Sign in to comment.

Accepted Answer

Rik on 30 Sep 2019
This is a good example of why you should be using functions. Your script is actually a function that could also be setting some default inputs. Why not make that explicit? That way you have a clear source of your variables and you can document the interface properly.


Joel Affolter
Joel Affolter on 30 Sep 2019
In what regard would that help me?
A total of 100 parameters need to be imported and are used for calculations, I do not find it particulary useful/userfriendly if all of these parameters need to be stated as the function input in the correct order.
If I were to change my script into a function, would that eliminate the error I am having on its own?
Rik on 30 Sep 2019
It probably would, because it would require explicit definition of inputs and (more importantly) outputs.
For such a large number of inputs I would suggest a struct as input, so you can easily add a missing fields with their default. I tend have 20 inputs at the most, but if I have more than 3 I tend to go for a struct (with a parser function that can also handle name value pairs).

Sign in to comment.

More Answers (0)