User Input with Loops

Hey guys,
I am wondering is there is a function that can read .xlsx, .csv, and .xls files. I wanted something that could work with the uigetfile('*.*') command For example, I want my user to select a spreadsheet either in .xlsx, .csv, or .xls format and send it through an analysis regardless of what they pick. Currently I have csvread, and was wondering if I could form an loop asking them what format the file is in and send it through it's respective analysis, whether it be xlsxread, xlsread, etc.
I apologize if the questions isn't clear.
Thanks!

 Accepted Answer

As far as I know, xlsread can potentially read all of them, provided they’re in Excel format. (The xlsread funciton calls Excel.) If you want to specifically select xlsread or csvread on the basis of the file extension, something like this would work:
filext = {'xls' 'xlsx' 'csv'};
fnm = inputdlg('Enter the full name (+ext) of your file: ');
ext = regexpi(fnm, '[.]?','split');
filtyp = find(strcmpi(ext{:}(2),filext));
followed by a switch-case block to call the appropriate function based on the output in ‘filtyp’, and passing to it the full ‘fnm’ file name.

7 Comments

mike
mike on 6 Dec 2014
Thank you so much for your help so far. I have run into a bit of a problem. After I entered the text you gave me, I am tried to put the selected file through the analysis below:
Raw_Force=csvread([filename],1,0,'A2..A1001');
[data] allows the selected file to be a user input just like I mentioned, but when I want to take items from the excel sheeet, it gives me the error:
Error using csvread (line 29) Filename must be a string.
Error in Final (line 21) Raw_Force=csvread([filename],1,0,'A2..A1001');
I used the brackets so the filename doesn't have to be a string.
How could I make it so the selected file is used in the csvread?
Thank you
My pleasure!
I forgot that inputdlg returns a cell. It didn’t come up in my original code because I kept it all as cells.
A minor change:
filext = {'xls' 'xlsx' 'csv'};
fnmc = inputdlg('Enter the full name (+ext) of your file: ');
fnms = char(fnmc);
ext = regexpi(fnmc, '[.]?','split');
filtyp = find(strcmpi(ext{:}(2),filext));
The input variable changed to ‘fnmc’ (‘file name cell’) and added ‘fnms’ (‘file name string’), now a character array. Use ‘fnms’ for your file name, and csvread should be happy.
mike
mike on 7 Dec 2014
Thanks a million! I really appreciate the help!
My pleasure!
The sincerest expression of appreciation here on MATLAB Answers is to Accept the answer that most closely solves your problem.
mike
mike on 7 Dec 2014
Would you happen to know how to convert .xls or .xlsx files to .csv?
You would have to save them as .csv files, either after having first read them in through MATLAB using xlsread, or directly saving them as .csv files in Excel.
See the documentation for csvwrite for details on writing .csv files in MATLAB.
Thanks!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 7 Dec 2014

0 votes

Depending on the way the data is laid out, you could use readtable(). importdata() should also be able to do it since it detects what's there and uses the appropriate lower level importing function (like csvread or xlsread).

Community Treasure Hunt

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

Start Hunting!