Using a file across GUI functions
3 views (last 30 days)
Show older comments
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
Accepted Answer
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.
More Answers (0)
See Also
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!