Trying to Import Excel Data using GUIDE push button

I am trying to create a GUI that contains a Push Button that finds an excel file, and then extracts the data and creates a .m file of the Data so that in the future rows can be selected and analyzed using the GUIDE GUI. I am able to find the file with the Push Button but once the file is selected and Open is pressed a series of errors come up.
Here is what I have ...
function pushbutton1_Callback(hObject, eventdata, handles)
path = 'c:\users\userName\my documents\matlab';
filter = '*.csv';
selectedFile = uigetfile(fullfile(path , filter));
num = xlsread(filename);

3 Comments

do you need that '*' as part of filter??? also what errors are you getting?
Tessa - what are the errors?
So I was able to manually find the file with the push button and then export it but now I need to find a way to instead find the file then take that file and import it into a .mat file. For example below is noted the InputInspectionFile.xlsx is manually entered and converted instead I want to grab the filename.xlsx that the user chose through file selection. Thanks guys!
% code
FilePath_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'.xlsx'},'File Selector');
fullpathname = strcat(pathname, filename);
text = fileread(fullpathname);
set(handles.FullPathName, 'String', fullpathname);
[num,txt,raw] = xlsread('InputInspectionFile.xlsx');
save('InputInspectionFile')
% code
end

Sign in to comment.

 Accepted Answer

Tessa - use fullfile rather than strcat to create the full file name from the path and name. If you want to save the data from the Excel file to a mat file, then why not just do
save('myData.mat','raw');
which will save the data extracted from your Excel file into a mat file named myData.mat.

16 Comments

Thank you for the fullfile. I updated my code as what I think you are saying and I am now getting an error when I click the button, then select the file, of Error:
Error using save
Variable 'raw' not found.
Error in UserInit>FilePath_Callback
save('myData.mat','raw');
My new Code is Below
% code
function FilePath_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'*.xlsx;*.xlsm;*.csv'},'File Selector');
fullpathname = fullfile(pathname, filename);
text = fileread(fullpathname);
set(handles.FullPathName, 'String', fullpathname);
save('myData.mat','raw');
end
Tessa - what happened to the following line of code
[num,txt,raw] = xlsread('InputInspectionFile.xlsx');
that was present in your last comment?
I took it away because what I would like is for the user to pick any file not just InputInspectionFile.mat, and then convert whatever the user had chosen. So the user clicks find maybe instead World.xlsx then takes automatically that file and converts to .mat or the user searched again and chose Hello.xlsx and then that file is converted and saved. Without having to write specifically in code what file because I will not know what the name is, there are many different names dependent on what the user wants to name it.
The code needs to take the users file of whatever there choosing as long as it is a .xlsx and convert in to .mat.
Right, but you still need the code
[num,txt,raw] = xlsread(....);
to read an Excel file so that you get the raw data so that you can save it to the mat file.
Maybe your code would become
FilePath_Callback(hObject, eventdata, handles)
% get the Excel file to read in
[filename pathname] = uigetfile({'.xlsx'},'File Selector');
excelFullfilename = fullfile(pathname, filename);
set(handles.FullPathName, 'String', excelFullfilename);
% read the data from the file
[num,txt,raw] = xlsread(excelFullfilename);
% get the file name parts
[pathstr,name,ext] = fileparts(excelFullfilename);
% create the mat filename equivalent
matFullfilename = fullfile(pathstr, [name '.mat']);
% save the data
save(matFullfilename,'raw');
I tried this and nothing gets saved.
Does a file get created with no data, or is there no file at all? Do you observe any errors in the command window?
Try putting a breakpoint in this above callback (say at the first line, uigetfile) and run your app. When the debugger pauses at this line, start stepping through the code. When you get to the save call, what is matFullfilename? what is raw? Are both valid?
No file gets created at all. There errors state "Undefined function or variable 'fullpathname'"; and "[num,txt,raw] = xlsread(fullpathname)" I think this is because xlsread() only allows specific file names. But I am no expert this is hair pulling! Thank you for being so quick and helpful.
Tessa - please post the code to this callback. Based on the error message, it seems that the fullpathname doesn't exist. Are you sure that you have declared it?
% code
function FilePath_Callback(hObject, eventdata, handles)
% get the Excel file to read in
[filename pathname] = uigetfile({'.xlsx'},'File Selector');
excelFullfilename = fullfile(pathname, filename);
set(handles.FullPathName, 'String', excelFullfilename);
% read the data from the file
[num,txt,raw] = xlsread(excelFullfilename);
% get the file name parts
[pathstr,name,ext] = fileparts(excelFullfilename);
% create the mat filename equivalent
matFullfilename = fullfile(pathstr, [name '.mat']);
% save the data
save(matFullfilename,'raw'); end
Tessa - the above code doesn't correspond to the error for "Undefined function or variable 'fullpathname'" with code
[num,txt,raw] = xlsread(fullpathname)
Are you sure that you have posted the correct code and/or correct error message?
You are correct I apologize, I created a different error on accident this is the correct error. Copy and pasted wrong...
Error using xlsread (line 251)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Excel cannot open the file 'InputInspectionFile.xlsx'
because the file format or file extension is not valid. Verify that
the file has not been corrupted and that the file extension matches
the format of the file.
Help File: xlmain11.chm
Help Context ID: 0
Error in UserInit>FilePath_Callback (line 52)
[num,txt,raw] = xlsread(excelFullfilename);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in UserInit (line 18)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)UserInit('FilePath_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
end
Tessa - is InputInspectionFile.xlsx a valid Excel file that you can open normally with Excel? So forget about MATLAB for the moment and try to open this file with Excel - what happens?
It is, when I use the code before you gave me yours it works perfectly for that specific file or any different file as long as the name is entered manually. But when your code is imputed this error shows up which leads me to believe it might have to do with Matlab's specific requirement for xlsread().
I can't help but wonder why the path to the file is not being prepended to the excelFullfilename. What is the pathname in
[filename pathname] = uigetfile({'.xlsx'},'File Selector');
There was something indeed wrong with the file but was not recognizable unless the file was opened not from Matlab. Your code worked, thank you so so much!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 3 Jun 2016

Commented:

on 14 Jun 2016

Community Treasure Hunt

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

Start Hunting!