Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
xlsread multiple xls files?

Subject: xlsread multiple xls files?

From: J favreau

Date: 21 Mar, 2004 22:17:36

Message: 1 of 12

I currently use xlsread to read data into my program. I have about a
hundred Excel files that need to be read. Is there any way to tell
my program to go into the folder with all the Excel files and
sequentially read each file, run the program on the file and then go
to the next file? thanks in advance, Jorie

Subject: xlsread multiple xls files?

From: A. Mosher

Date: 22 Mar, 2004 05:34:33

Message: 2 of 12

J favreau wrote:
>
>
> I currently use xlsread to read data into my program. I have about
> a
> hundred Excel files that need to be read. Is there any way to tell
> my program to go into the folder with all the Excel files and
> sequentially read each file, run the program on the file and then
> go
> to the next file? thanks in advance, Jorie

You can try something like:

%use cd to change to the directory you want

FileNames=dir('*.xls);
%this gets all the file names in current director that have extension
.xls, as a structure

for i=1:length(FileNames)
FileToLoad=FileNames(i).name;
%this is a xls file, load it, run code and loop
end

HTH!!

Subject: xlsread multiple xls files?

From: A. Mosher

Date: 22 Mar, 2004 05:36:55

Message: 3 of 12

%use cd to change to the directory you want
>
> FileNames=dir('*.xls);
> %this gets all the file names in current director that have
> extension
> .xls, as a structure
>
> for i=1:length(FileNames)
> FileToLoad=FileNames(i).name;
> %this is a xls file, load it, run code and loop
> end
>
> HTH!!

small error in this code

FileNames=dir('*.xls');

I forgot the ' in the first post.

Subject: xlsread multiple xls files?

From: J favreau

Date: 22 Mar, 2004 08:39:05

Message: 4 of 12

great, thanks!

a. mosher wrote:
>
>
> %use cd to change to the directory you want
>>
>> FileNames=dir('*.xls);
>> %this gets all the file names in current director that have
>> extension
>> .xls, as a structure
>>
>> for i=1:length(FileNames)
>> FileToLoad=FileNames(i).name;
>> %this is a xls file, load it, run code and loop
>> end
>>
>> HTH!!
>
> small error in this code
>
> FileNames=dir('*.xls');
>
> I forgot the ' in the first post.

Subject: xlsread multiple xls files?

From: Johan

Date: 30 Oct, 2012 13:55:09

Message: 5 of 12

"J favreau" <jmfavrea@unity.ncsu.edu> wrote in message <eed426f.2@webx.raydaftYaTP>...
> great, thanks!
>
> a. mosher wrote:
> >
> >
> > %use cd to change to the directory you want
> >>
> >> FileNames=dir('*.xls);
> >> %this gets all the file names in current director that have
> >> extension
> >> .xls, as a structure
> >>
> >> for i=1:length(FileNames)
> >> FileToLoad=FileNames(i).name;
> >> %this is a xls file, load it, run code and loop
> >> end
> >>
> >> HTH!!
> >
> > small error in this code
> >
> > FileNames=dir('*.xls');
> >
> > I forgot the ' in the first post.

Hi Favreau,

I tried something similar before, until I found this thread an hoped to get a solution for my problem. But it still doesn't work for me.

When I tell matlab to search all files with endings .xlsx, I get a matrix with all information about my excel file, including their filenames.
As you explained, I decalare a variable called FileToLoad, with the name of my excelfile.
When I then try to use xlsread('FileToLoad') it doesnt work cause matlab can't find a file called FileToLoad.
Thats right so far, but why doesnt matlab simply use the information from the variable 'FileToLoad' instead... for example (mydata.xlsx).
Seemingly it worked for you, but I cant figure out why, so I would appreciate if you can help me with my problem.

Thanks

Johan

Subject: xlsread multiple xls files?

From: Johan

Date: 30 Oct, 2012 14:04:08

Message: 6 of 12

"Johan" wrote in message <k6om7t$30b$1@newscl01ah.mathworks.com>...
> "J favreau" <jmfavrea@unity.ncsu.edu> wrote in message <eed426f.2@webx.raydaftYaTP>...
> > great, thanks!
> >
> > a. mosher wrote:
> > >
> > >
> > > %use cd to change to the directory you want
> > >>
> > >> FileNames=dir('*.xls);
> > >> %this gets all the file names in current director that have
> > >> extension
> > >> .xls, as a structure
> > >>
> > >> for i=1:length(FileNames)
> > >> FileToLoad=FileNames(i).name;
> > >> %this is a xls file, load it, run code and loop
> > >> end
> > >>
> > >> HTH!!
> > >
> > > small error in this code
> > >
> > > FileNames=dir('*.xls');
> > >
> > > I forgot the ' in the first post.
>
> Hi Favreau,
>
> I tried something similar before, until I found this thread an hoped to get a solution for my problem. But it still doesn't work for me.
>
> When I tell matlab to search all files with endings .xlsx, I get a matrix with all information about my excel file, including their filenames.
> As you explained, I decalare a variable called FileToLoad, with the name of my excelfile.
> When I then try to use xlsread('FileToLoad') it doesnt work cause matlab can't find a file called FileToLoad.
> Thats right so far, but why doesnt matlab simply use the information from the variable 'FileToLoad' instead... for example (mydata.xlsx).
> Seemingly it worked for you, but I cant figure out why, so I would appreciate if you can help me with my problem.
>
> Thanks
>
> Johan

Sorry, I mixed it up, i meant A. Mosher... but if you have some hints for me too, I wouldn't reject them

Subject: xlsread multiple xls files?

From: dpb

Date: 30 Oct, 2012 14:24:01

Message: 7 of 12

On 10/30/2012 8:55 AM, Johan wrote:
> "J favreau" <jmfavrea@unity.ncsu.edu> wrote in message
> <eed426f.2@webx.raydaftYaTP>...
>> a. mosher wrote:
...
>> >> FileNames=dir('*.xls);
...
>> >> for i=1:length(FileNames)
>> >> FileToLoad=FileNames(i).name;
>> >> %this is a xls file, load it, run code and loop
>> >> end
...

> I tried something similar before, until I found this thread an hoped to
> get a solution for my problem. But it still doesn't work for me.
> When I tell matlab to search all files with endings .xlsx, I get a
> matrix with all information about my excel file, including their
> filenames.

More specifically, DIR returns a structure...

>... As you explained, I decalare a variable called FileToLoad,
> with the name of my excelfile.
> When I then try to use xlsread('FileToLoad') it doesnt work cause matlab
> can't find a file called FileToLoad.
> Thats right so far, but why doesnt matlab simply use the information
> from the variable 'FileToLoad' instead...

Well, that's because 'FileToLoad' isn't a variable but a character string...

If you really did write

FileToLoad='mydata.xlsx';

then use the variable name not a string equal to the variable name.

> Seemingly it worked for you, but I cant figure out why, so I would
> appreciate if you can help me with my problem.

Well, it worked for the poster because he used the .name field in the
structure returned by DIR() instead of a string of that name...

Which I'd recommend you do as well--much more flexible coding style than
hardcoding a filename into a script or function. Or, use uigetfile() to
select a file interactively or somesuch.

--


> Thanks
>
> Johan

Subject: xlsread multiple xls files?

From: Steven_Lord

Date: 30 Oct, 2012 14:31:36

Message: 8 of 12



"Johan " <etk50505@stud.uni-stuttgart.de> wrote in message
news:k6om7t$30b$1@newscl01ah.mathworks.com...

*snip*

> I tried something similar before, until I found this thread an hoped to
> get a solution for my problem. But it still doesn't work for me.
> When I tell matlab to search all files with endings .xlsx, I get a matrix
> with all information about my excel file, including their filenames. As
> you explained, I decalare a variable called FileToLoad, with the name of
> my excelfile.
> When I then try to use xlsread('FileToLoad') it doesnt work cause matlab
> can't find a file called FileToLoad.

That's correct. xlsread('FileToLoad') attempts to read in a file with the
name FileToLoad and a "supported Excel extension." xlsread(FileToLoad)
attempts to read in the file whose name is stored in the variable
FileToLoad.

> Thats right so far, but why doesnt matlab simply use the information from
> the variable 'FileToLoad' instead... for example (mydata.xlsx).

Because MATLAB does what you tell it to do. We try to limit the
circumstances under which MATLAB does what it _thinks you meant_ instead of
what you _told it to do_ because it's easy to get the "mind reading" part of
that wrong. [Maybe once the Mind Reading Toolbox is operational ...]

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: xlsread multiple xls files?

From: Johan

Date: 30 Oct, 2012 14:45:08

Message: 9 of 12

"Steven_Lord" <slord@mathworks.com> wrote in message <k6ooc8$c75$1@newscl01ah.mathworks.com>...
>
>
> "Johan " <etk50505@stud.uni-stuttgart.de> wrote in message
> news:k6om7t$30b$1@newscl01ah.mathworks.com...
>
> *snip*
>
> > I tried something similar before, until I found this thread an hoped to
> > get a solution for my problem. But it still doesn't work for me.
> > When I tell matlab to search all files with endings .xlsx, I get a matrix
> > with all information about my excel file, including their filenames. As
> > you explained, I decalare a variable called FileToLoad, with the name of
> > my excelfile.
> > When I then try to use xlsread('FileToLoad') it doesnt work cause matlab
> > can't find a file called FileToLoad.
>
> That's correct. xlsread('FileToLoad') attempts to read in a file with the
> name FileToLoad and a "supported Excel extension." xlsread(FileToLoad)
> attempts to read in the file whose name is stored in the variable
> FileToLoad.
>
> > Thats right so far, but why doesnt matlab simply use the information from
> > the variable 'FileToLoad' instead... for example (mydata.xlsx).
>
> Because MATLAB does what you tell it to do. We try to limit the
> circumstances under which MATLAB does what it _thinks you meant_ instead of
> what you _told it to do_ because it's easy to get the "mind reading" part of
> that wrong. [Maybe once the Mind Reading Toolbox is operational ...]
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thank you guys!

I could swear, I tried this before...
Really stupid of me, when i really forget to try the simplest solution.

Subject: xlsread multiple xls files?

From: Ryan

Date: 2 Nov, 2012 22:02:08

Message: 10 of 12

Read All Files in a Folder:

Data Import and Export:
http://www.mathworks.com/help/pdf_doc/matlab/import_export.pdf


What is MATLAB?:
http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F



"Johan" wrote in message <k6op5k$fhj$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <k6ooc8$c75$1@newscl01ah.mathworks.com>...
> >
> >
> > "Johan " <etk50505@stud.uni-stuttgart.de> wrote in message
> > news:k6om7t$30b$1@newscl01ah.mathworks.com...
> >
> > *snip*
> >
> > > I tried something similar before, until I found this thread an hoped to
> > > get a solution for my problem. But it still doesn't work for me.
> > > When I tell matlab to search all files with endings .xlsx, I get a matrix
> > > with all information about my excel file, including their filenames. As
> > > you explained, I decalare a variable called FileToLoad, with the name of
> > > my excelfile.
> > > When I then try to use xlsread('FileToLoad') it doesnt work cause matlab
> > > can't find a file called FileToLoad.
> >
> > That's correct. xlsread('FileToLoad') attempts to read in a file with the
> > name FileToLoad and a "supported Excel extension." xlsread(FileToLoad)
> > attempts to read in the file whose name is stored in the variable
> > FileToLoad.
> >
> > > Thats right so far, but why doesnt matlab simply use the information from
> > > the variable 'FileToLoad' instead... for example (mydata.xlsx).
> >
> > Because MATLAB does what you tell it to do. We try to limit the
> > circumstances under which MATLAB does what it _thinks you meant_ instead of
> > what you _told it to do_ because it's easy to get the "mind reading" part of
> > that wrong. [Maybe once the Mind Reading Toolbox is operational ...]
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> Thank you guys!
>
> I could swear, I tried this before...
> Really stupid of me, when i really forget to try the simplest solution.

Subject: xlsread multiple xls files?

From: Ryan

Date: 3 Nov, 2012 02:34:11

Message: 11 of 12

I just tried this; it works:
folders = {'C:\Users\Excel\Desktop\Ryan_Folder\'};
DATA = cell(numel(folders),1);
 
for fo = 1:numel(folders)
    files = dir(folders{fo}); %# get array fo all files in a folder
    files([files(:).isdir]) = []; %# remove directories from the structure array
    DATA{fo} = cell(numel(files),1);
    for fi = 1:numel(files)
        filename = fullfile(folders{fo},files(fi).name);
        disp(filename)
        [status,sheets] = xlsfinfo(filename);
        if ~isempty(status)
            lastsheet = sheets{end};
            [num,txt,raw] = xlsread(filename,lastsheet);
            DATA{fo}{fi} = num; %# or txt, or raw
        end
    end
end


"Ryan" wrote in message <k71ft0$og2$1@newscl01ah.mathworks.com>...
> Read All Files in a Folder:
>
> Data Import and Export:
> http://www.mathworks.com/help/pdf_doc/matlab/import_export.pdf
>
>
> What is MATLAB?:
> http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
>
>
>
> "Johan" wrote in message <k6op5k$fhj$1@newscl01ah.mathworks.com>...
> > "Steven_Lord" <slord@mathworks.com> wrote in message <k6ooc8$c75$1@newscl01ah.mathworks.com>...
> > >
> > >
> > > "Johan " <etk50505@stud.uni-stuttgart.de> wrote in message
> > > news:k6om7t$30b$1@newscl01ah.mathworks.com...
> > >
> > > *snip*
> > >
> > > > I tried something similar before, until I found this thread an hoped to
> > > > get a solution for my problem. But it still doesn't work for me.
> > > > When I tell matlab to search all files with endings .xlsx, I get a matrix
> > > > with all information about my excel file, including their filenames. As
> > > > you explained, I decalare a variable called FileToLoad, with the name of
> > > > my excelfile.
> > > > When I then try to use xlsread('FileToLoad') it doesnt work cause matlab
> > > > can't find a file called FileToLoad.
> > >
> > > That's correct. xlsread('FileToLoad') attempts to read in a file with the
> > > name FileToLoad and a "supported Excel extension." xlsread(FileToLoad)
> > > attempts to read in the file whose name is stored in the variable
> > > FileToLoad.
> > >
> > > > Thats right so far, but why doesnt matlab simply use the information from
> > > > the variable 'FileToLoad' instead... for example (mydata.xlsx).
> > >
> > > Because MATLAB does what you tell it to do. We try to limit the
> > > circumstances under which MATLAB does what it _thinks you meant_ instead of
> > > what you _told it to do_ because it's easy to get the "mind reading" part of
> > > that wrong. [Maybe once the Mind Reading Toolbox is operational ...]
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > > To contact Technical Support use the Contact Us link on
> > > http://www.mathworks.com
> >
> > Thank you guys!
> >
> > I could swear, I tried this before...
> > Really stupid of me, when i really forget to try the simplest solution.

Subject: xlsread multiple xls files?

From: Ryan

Date: 3 Nov, 2012 02:45:14

Message: 12 of 12

You can try this as well!!!

%# get input XLS files
dName = uigetdir('.', 'Select folder containing Excel XLS files');
if dName==0, error('No folder selected'); end
files = dir( fullfile(dName,'*.xls') );
files = strcat(dName, filesep, {files.name}'); %'
 
%# prepare output XLS file
[fName dName] = uiputfile({'*.xls' 'Excel (*.xls)'}, 'Output File', 'final.xls');
if dName==0, error('No file selected'); end
fOut = fullfile(dName,fName);
 
%# process
NUM_SHEETS = 5; %# number of sheets per file
for s=1:NUM_SHEETS
    %# extract contents of same sheet from all files
    numData = cell(numel(files),1);
    for f=1:numel(files)
        numData{f} = xlsread(files{f}, s);
    end
 
    %# rearrange data
    numData = cat(3,numData{:});
    numData = reshape(permute(numData,[3 1 2]), [], size(numData,2));
 
    %# write data to corresponding sheet of output XLS file
    xlswrite(fOut, numData, s);
end



"Ryan" wrote in message <k71ft0$og2$1@newscl01ah.mathworks.com>...
> Read All Files in a Folder:
>
> Data Import and Export:
> http://www.mathworks.com/help/pdf_doc/matlab/import_export.pdf
>
>
> What is MATLAB?:
> http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
>
>
>
> "Johan" wrote in message <k6op5k$fhj$1@newscl01ah.mathworks.com>...
> > "Steven_Lord" <slord@mathworks.com> wrote in message <k6ooc8$c75$1@newscl01ah.mathworks.com>...
> > >
> > >
> > > "Johan " <etk50505@stud.uni-stuttgart.de> wrote in message
> > > news:k6om7t$30b$1@newscl01ah.mathworks.com...
> > >
> > > *snip*
> > >
> > > > I tried something similar before, until I found this thread an hoped to
> > > > get a solution for my problem. But it still doesn't work for me.
> > > > When I tell matlab to search all files with endings .xlsx, I get a matrix
> > > > with all information about my excel file, including their filenames. As
> > > > you explained, I decalare a variable called FileToLoad, with the name of
> > > > my excelfile.
> > > > When I then try to use xlsread('FileToLoad') it doesnt work cause matlab
> > > > can't find a file called FileToLoad.
> > >
> > > That's correct. xlsread('FileToLoad') attempts to read in a file with the
> > > name FileToLoad and a "supported Excel extension." xlsread(FileToLoad)
> > > attempts to read in the file whose name is stored in the variable
> > > FileToLoad.
> > >
> > > > Thats right so far, but why doesnt matlab simply use the information from
> > > > the variable 'FileToLoad' instead... for example (mydata.xlsx).
> > >
> > > Because MATLAB does what you tell it to do. We try to limit the
> > > circumstances under which MATLAB does what it _thinks you meant_ instead of
> > > what you _told it to do_ because it's easy to get the "mind reading" part of
> > > that wrong. [Maybe once the Mind Reading Toolbox is operational ...]
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > > To contact Technical Support use the Contact Us link on
> > > http://www.mathworks.com
> >
> > Thank you guys!
> >
> > I could swear, I tried this before...
> > Really stupid of me, when i really forget to try the simplest solution.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us