Using a file across GUI functions

3 views (last 30 days)
Emma
Emma on 30 Jan 2015
Edited: Emma on 30 Jan 2015
Hi, I've done a lot of trying to find an answer to my question and can't find a solution.
I'm making a GUI which I want to load in a file (of EEG data) when a pushbutton is pressed, and then be able to call this later on to use in other functions. Below is my code at the moment. I then want the file that I've clicked on when "open" appears from uigetfile to actually be available in the workspace etc. I assume it has something to do with handles.
I hope this makes sense. I'm using GUIDE and very new to it, any help greatly appreciated.
Thanks, Emma
function Load_Callback(hObject, eventdata, handles)
[filename pathname]=uigetfile({'*.*', 'matlab'});
fullfile = strcat(pathname, filename);
load([num2str(fullfile)])
set(handles.data_load, 'String', fullfile) %shows full path name in a text box
set(handles.listbox1, 'String', filename) %shows filename in data list box
  2 Comments
Jan
Jan on 30 Jan 2015
Edited: Jan on 30 Jan 2015
Better use fullfile as command instead of strcat.
You want to be able to call what later on? The file name or the contents of the file?
Emma
Emma on 30 Jan 2015
Edited: Emma on 30 Jan 2015
Okay thanks. The contents of the file but also the name for display purposes. But the data contained is what I really need.

Sign in to comment.

Accepted Answer

Jan
Jan on 30 Jan 2015
If you really want to write the contents of the file to the base workspace:
File = fullfile(pathname, filename);
Data = load(File);
assignin('base', 'Data', Data);
Now the variable "Data" is created in the base workspace. I strongly recommend not to import the contents of the file directly without catching the output of the load command. Otherwise it gets a horror to debug your code. What might happen if the MAT file contains a variable which is called "exit" and you want to quit Matlab?
The clean way would be to store the data in the figures UserData oder ApplicationData. Then a specific function can extract it, when other callbacks or GUIs want to process it. Poofing the base workspace is like using global variables. It works, when the program is small and only one program is used. But as soon as 2 GUIs are open at the same time, there is no chance to prevent collisions, if the variables have the same name.
  1 Comment
Emma
Emma on 30 Jan 2015
Edited: Emma on 30 Jan 2015
Thank you, I have done that and get my data in a structure "Data". How do I then extract the elements from the structure in a later function? - I get the "undefined function/variable" error message.
EDIT: struct2array! Have managed it, thanks for your help :)

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!