How to import numerous excel files into MATLAB?

A have several excel files that I need to modify through MatLab and export into a different folder. I need help with importing and exporting an entire folder of excel files.
Thanks,
-Frank
source_dir = 'C:\Users\xuf\Desktop\Excel Saved PointScans'
dest_dir = 'C:\Users\xuf\Desktop\Test Folder'
source_files = dir(fullfile(source_dir, '*.xls'));
for i = 1:length(source_files)
data = xlsread(fullfile(source_dir,source_files(i).name));
xlswrite(fullfile(dest_dir,source_files(i).name));
end

 Respuesta aceptada

Will Dampier
Will Dampier el 10 de Mayo de 2011
You won't be able to load them all at once but you could easily use a for-loop to process them one at a time. Something like this:
source_dir = 'path/to/source/'
dest_dir = '/path/to/dest'
source_files = dir(fullfile(source_dir, '*.xls'));
for i = 1:length(source_files)
data = xlsread(fullfile(source_dir, source_files(i).name)));
#do something with data
xlswrite(fullfile(dest_dir, source_files(i).name)));
end

14 comentarios

Frank
Frank el 10 de Mayo de 2011
Hi
Would I have to individually input the name of the file where the Astrix is on the 3rd line?
Thanks,
-Frank
Andy
Andy el 10 de Mayo de 2011
No. That argument to the dir function tells it to return all files ("all" symbolized by the *) with extension .xls. Perhaps it would be helpful to read the documentation for dir by typing:
doc dir
at the command line.
Frank
Frank el 10 de Mayo de 2011
Hi
Okay, however, I still have trouble setting up the script. I've tried the code above from Will, however, I get no output in the destination folder.
-Frank
Oleg Komarov
Oleg Komarov el 10 de Mayo de 2011
Edit your original post adding the code formatted with the "{} code" button.
Frank
Frank el 10 de Mayo de 2011
Hi
I basically only have the code above, (I'm not very good at matlab).
-Frank
Oleg Komarov
Oleg Komarov el 10 de Mayo de 2011
Did you substitute 'path/to/source' and 'path/to/dest' with the paths of interest?
Frank
Frank el 10 de Mayo de 2011
Yes
Oleg Komarov
Oleg Komarov el 10 de Mayo de 2011
Did you get an error?
Frank
Frank el 10 de Mayo de 2011
I'm getting:
???Error using ==>xlswrite at 151
Not enough input arguments
Error in ==> testing4 at 6
xlswrite(fullfile(dest_dir,source_files(i).name));
Andy
Andy el 10 de Mayo de 2011
You can see the help for xlswrite by typing "doc xlswrite" at the command line. You would see that xlswrite takes two arguments: the file and the variable to be stored. So you should change that line to:
xlswrite(fullfile(dest_dir,source_files(i).name), data);
Frank
Frank el 10 de Mayo de 2011
Hey!
That worked perfectly, thank you guys!
-Frank
Thanks: Oleg, Andy, and Will!
Jeong_evolution
Jeong_evolution el 4 de Jul. de 2016
But this code just imported numeric data.
I want to import from numeric to txt data.
[num txt raw] = xlsread('') is just imported only 1 files.
What should I do?
Image Analyst
Image Analyst el 4 de Jul. de 2016
You need to pass it a filename. You need to call it multiple times if you have multiple filenames.
Nicolò Biagi
Nicolò Biagi el 22 de Feb. de 2018
can you give us an example on how to do it?
Thanks

Iniciar sesión para comentar.

Más respuestas (2)

Michael Chavez
Michael Chavez el 20 de Feb. de 2015

1 voto

This has been a helpful thread for a project that I am working on. So thank you, Frank, for asking it and thank you to all that have responded.
I would like to add and ask is it possible to change the names of the output files from xlswrite, rather than the output file being the same name as the corresponding input file?
Arnaud, I was looking at the documentation for dir and I'm not sure that I saw how to generate a name for the list of output files or maybe I am overlooking the function to change the name. I am using MATLAB 7.8.0 (R2009a) for the record.
Arnaud Miege
Arnaud Miege el 10 de Mayo de 2011

0 votos

Have a look at the documentation and use dir to generate the list of file names in the folder.
HTH,
Arnaud

Preguntada:

el 10 de Mayo de 2011

Comentada:

el 22 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by