Using a file across GUI functions

2 次查看(过去 30 天)
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 个评论
Jan
Jan 2015-1-30
编辑:Jan 2015-1-30
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 2015-1-30
编辑:Emma 2015-1-30
Okay thanks. The contents of the file but also the name for display purposes. But the data contained is what I really need.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2015-1-30
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 个评论
Emma
Emma 2015-1-30
编辑:Emma 2015-1-30
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 :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by