How to pass a function workspace into a guidata handles structure in a GUI
4 views (last 30 days)
Show older comments
Hi guys, I have a function called within a GUI structure callback built with GUIDE that produces a significant number of variables in that function's workspace, and I'd like to use guidata to pass a structure of workspace variables to other callbacks but I can't get the syntax right. In the GUI's opening function I've defined a handles structure called handles.Drive in which I'd like to store the structure:
% --- Executes just before GUIv01b is made visible.
function GUIv01b_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUIv01b (see VARARGIN)
% Choose default command line output for GUIv01b
handles.output = hObject;
%Create handles structure to store all data gererated by Drive_Matlab.m
handles.Drive = hObject;
% Update handles structure
guidata(hObject, handles);
Later the callback containing the function is called, and it's the workspace variables produced from a script called Drive_Matlab that I want to pass to handles.Drive:
% --- Executes on button press in GUILoadXLSXFile.
function GUILoadXLSXFile_Callback(hObject, eventdata, handles)
% hObject handle to GUILoadXLSXFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clearvars except (hObject,eventdata,handles);
clear handles.GUIThreatTable.Data;
clear handles.STSProfileDataTable.Data;
clear handles.PlotData, 'Data';
cla reset;
Drive_Matlab; %Assign all workspace variables to handles.Drive
save Drive;
handles.Drive = load(Drive);
guidata(hObject,handles);
The code above represents an attempt to save all workspace variables as Drive.mat and then pull that into the handles structure, but that doesn't seem to work either. A question that just occurred to me: do I need to populate the handles structure handles.Drive from within the function that generates the variables, or is it sufficient to do it from within the GUI callback that contains the call to the function?
Thanks, Jason
8 Comments
Stephen23
on 1 Feb 2018
Edited: Stephen23
on 1 Feb 2018
'a response along the lines of "write better code and you won't have this problem" is spectacularly UNhelpful'
Thank you for your feedback. Users of this forum are often pleased to learn about simpler and more efficient ways to write their code, and they appreciate the advantages to their own lives (simpler debugging, faster code, etc). I understand that badly-written legacy code is sometimes a "fact of life" beyond our control, but nowhere in your question is this situation explained: it might surprise you to know that we do not posses the magic ability to know the entire background to your task, nor can we see your monitor and all of the code that you are running. We made recommendations based on what you told us, which to my mind was appropriate.
"A couple of lines of code illustrating the proper syntax for what I was trying to do would have been far more helpful and saved a couple more hours of trial and error."
My "spectacularly UNhelpful" comment (which was not provided as an answer to your question) contained a link to a page that lists the main ways to solve your problem. Did you read it?
"It's not as though handing me some code to address my specific question would have been akin to handing a suicidal person a straight razor"
You are right: it would be akin to handing them a chainsaw:
Accepted Answer
Rik
on 31 Jan 2018
Why don't you convert Drive_Matlab to a function that returns its variable as fields of a struct? You can put the save/load trick in that script (now function), or better yet, replace it by a more robust method that doesn't rely on the very slow method of writing to disk and reading from disk.
0 Comments
More Answers (0)
See Also
Categories
Find more on Whos 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!