Using a file across GUI functions

조회 수: 10 (최근 30일)
Emma
Emma 2015년 1월 30일
편집: Emma 2015년 1월 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에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by