Importing a foler of CSV files into matlab
43 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I am trying to import a folder (groupB) which contains a large number of csv files named group01.csv, group02.csv and so on but the code I am inputting just returns a blank matrix. Here is my code:
JumpFiles = dir('E:\groupB/*.CSV');
Any help on why this won't return the files?
Thanks
0 comentarios
Respuestas (4)
Luke Perry
el 6 de Jun. de 2018
Editada: Luke Perry
el 3 de Ag. de 2018
The easiest way I've found on doing this would be something like the following:
%Get information about what's inside your folder.
myfiles = dir('C:\The_file_location_folder');
%Get the filenames and folders of all files and folders inside the folder
%of your choice.
filenames={myfiles(:).name}';
filefolders={myfiles(:).folder}';
%Get only those files that have a csv extension and their corresponding
%folders.
csvfiles=filenames(endsWith(filenames,'.csv'));
csvfolders=filefolders(endsWith(filenames,'.csv'));
%Make a cell array of strings containing the full file locations of the
%files.
files=fullfile(csvfolders,csvfiles);
Now after this you can use whatever you'd like to loop through those files and open them, whether it be xlsread() or fopen() with textscan().
Personally, I prefer using textscan() to open .csv files because it takes quite a bit less time, and it sounds as though you may have a large number of files. However, textscan does have its drawbacks, and if the files were created in Excel, it may not always format whatever's inside the files properly, not allowing you to actually get the information you desire out of it.
See these examples for textscan() and xlsread().
textscan():
xlsread()
You may also find other resources on how to read in csv files quickly, such as csvread(). However, I've always preferred to design my own routines to read in the information I am looking for.
2 comentarios
Luke Perry
el 3 de Ag. de 2018
Thank you, Jan. Good catch. I've adjusted my answer with both of your suggestions.
Sean de Wolski
el 14 de Mayo de 2013
If they all have that naming scheme how about using a for loop:
for ii = 1:10
fn = strcat('E:\group',num2str(ii,'%02i'),'.csv')
end
4 comentarios
Sean de Wolski
el 14 de Mayo de 2013
Well that's because nothing else happens inside the loop.
You would have to include the csvread or whatever command at each iteration in order to read it in while fn is equal to that name...
Seth
el 15 de Mayo de 2013
Any time I use csvread to import the whole folder it says the variable doesn't exist. Using csvread allows me to import single files inside the folder 'groupB01.csv' for instance but the folder 'groupB' doesn't return anything
Jan
el 14 de Mayo de 2013
Editada: Jan
el 14 de Mayo de 2013
Are you working under the case-sensitive Linux system?
When dir('E:\groupB\*.csv') does not find any files, there are no such files. So please explain, why you expect the files to exist.
2 comentarios
Seth
el 14 de Mayo de 2013
the files are downloaded and saved in a folder named groupB which has all the csv files in it but they do not appear when I try to read them in as a group. I can import indivudual csv files from the group by writing:
csvread('groupB01.CSV',6,1);
But I have not been able to do import the whole folder, any hints?
Ken Atwell
el 15 de Mayo de 2013
I believe CSVREAD can only read one file. You would need to create a loop to read all of the files. Something like (this written from memory):
cd <folder/where/files/are>
files = dir('*.csv');
outs = cell(numel(files),1);
for i = 1:numel(files) % Could be parfor
out{i} = csvread(files(i).name, 6.1)
end
% Merge the outputs into one bigger matrix
allouts = vertcat(out{:});
David Sanchez
el 14 de Mayo de 2013
Are your CSVs in the working directory? my_csvs = dir('*.CSV') should return a struct array with a field called "name", hosting the name of the *.csv file in each case. If your *.csv files are somewhere else, try to retrieve the path with
my_csv_dir = uigetdir;
what will prompt a search directory window and return the directory of your choice. Then:
my_full_path = horzcat(my_csv_dir,'\group*.CSV')
my_csv_files_struct = dir(my_full_path);
1 comentario
Seth
el 14 de Mayo de 2013
Yes the folder with the CSV files is in the working directory yet it does not import them all in
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!