Trying to Import Excel Data using GUIDE push button
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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
Jake G
on 3 Jun 2016
do you need that '*' as part of filter??? also what errors are you getting?
Geoff Hayes
on 3 Jun 2016
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
Accepted Answer
Geoff Hayes
on 8 Jun 2016
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
Tessa Aus
on 8 Jun 2016
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
Geoff Hayes
on 8 Jun 2016
Tessa - what happened to the following line of code
[num,txt,raw] = xlsread('InputInspectionFile.xlsx');
that was present in your last comment?
Tessa Aus
on 8 Jun 2016
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.
Tessa Aus
on 8 Jun 2016
The code needs to take the users file of whatever there choosing as long as it is a .xlsx and convert in to .mat.
Geoff Hayes
on 8 Jun 2016
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');
Tessa Aus
on 8 Jun 2016
I tried this and nothing gets saved.
Geoff Hayes
on 8 Jun 2016
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?
Tessa Aus
on 9 Jun 2016
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.
Geoff Hayes
on 9 Jun 2016
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
Geoff Hayes
on 9 Jun 2016
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?
Tessa Aus
on 9 Jun 2016
Edited: Geoff Hayes
on 9 Jun 2016
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
Geoff Hayes
on 9 Jun 2016
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?
Tessa Aus
on 10 Jun 2016
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().
Geoff Hayes
on 10 Jun 2016
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');
Tessa Aus
on 14 Jun 2016
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!
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)