Importing a foler of CSV files into matlab

69 次查看(过去 30 天)
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

回答(4 个)

Luke Perry
Luke Perry 2018-6-6
编辑:Luke Perry 2018-8-3
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():
Specific Data Formats: Data Format
Some problems you may encounter: Problem 1 Problem 2
xlsread()
Online Documentation: Mathworks Documentation
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 个评论
Jan
Jan 2018-6-6
编辑:Jan 2018-6-6
contains(filenames,'.csv')
This catches "myfile.csv.pdf" also. Use endsWith instead.
Use fullfile instead of strcat, because this considers e.g. 'C:\' without inserting a double file separator.
Luke Perry
Luke Perry 2018-8-3
Thank you, Jan. Good catch. I've adjusted my answer with both of your suggestions.

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2013-5-14
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 个评论
Sean de Wolski
Sean de Wolski 2013-5-14
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
Seth 2013-5-15
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
Jan 2013-5-14
编辑:Jan 2013-5-14
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 个评论
Seth
Seth 2013-5-14
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
Ken Atwell 2013-5-15
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
David Sanchez 2013-5-14
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 个评论
Seth
Seth 2013-5-14
Yes the folder with the CSV files is in the working directory yet it does not import them all in

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by