Global variables and importing a file. How do functions pass variables?
Mostrar comentarios más antiguos
Why can't I import the file after making it a global variable?
Here is the error:
??? Undefined function or method 'fieldnames' for input arguments of type
'cell'.
Error in ==> joan2>pushbutton1_Callback at 124
vars = fieldnames(D);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> joan2 at 43
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)joan2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Here is the snippet of code:
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 file(see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global file
file = uigetfile('*.mat', 'Select MAT-file');
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global file
DELIMITER = ',';
HEADERLINES = 98;
% Import the file
D = importdata(file, DELIMITER, HEADERLINES);
vars = fieldnames(D);
for i = 1:length(vars)
assignin('base', vars{i}, D.(vars{i}));
end
Many, many thanks to whoever can solve this problem!
Respuestas (1)
Walter Roberson
el 25 de Abr. de 2012
0 votos
Are you choosing a file from a different directory? If so then you need to record the path as well as the file name.
Meanwhile, if you load a .mat file and that file only has a single variable in it, then the return value of importdata will be the content of the variable itself, with no structure imposed on top of it. If the variable happened to be a cell, importdata would return the cell.
You thus cannot predict the output class from importdata except by using whos -file to examine the .mat file to see whether there happens to be only one variable in the .mat file.
I would suggest that you use load() instead of importdata(), as load() assigned to an output variable will always create a structure field for every variable stored in the .mat file, even if there is only one variable stored there.
3 comentarios
J
el 25 de Abr. de 2012
Evan
el 25 de Abr. de 2012
You can get the file name and path by specifying two outputs from uigetfile and then using fullfile to create a string of the path + filename.
So something like this:
[filename pathname] = uigetfile('*.mat','Select MAT-file');
file = fullfile(pathname,name);
Also see:
help uigetfile
help fullfile
Walter Roberson
el 25 de Abr. de 2012
The only way that uigetfile() can return a cell array of strings is if you use the MultiSelect option in uigetfile().
You *are* importing the data. If the file was not found to be able to open it, then you would have received an error message such as
>> foo = importdata('yafaf.mat')
??? Error using ==> importdata at 115
Unable to open file.
The problem you are encountering is that importdata() is not returning structure array for you. Above I told you how that can happen: if the .mat file contains exactly one variable, then that variable's value is returned, in whatever class it happens to be, with no structure wrapped around it. This does not occur if there are two or more variables in the .mat file: if there are two or more files then importdata() always returns a structure array.
The fix is not to worry about uigetfile() possibly returning a cell array of strings: the fix is to use
D = load(file);
instead of using importdata().
While you are changing the code, it is strongly recommended that you do not blindly assignin('base') according to the names of the variables stored in the .mat file, as you could end up overwriting other variables you need. Instead, leave everything in a structure and at most assign the structure as a whole into the base workstation, and then access the structure fields where you need them.
You should get used to programming defensively, under the assumption that the user has deliberately constructed .mat files with variable names that you use for your own purposes.
Categorías
Más información sobre Large Files and Big Data en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!