How to import multiple csv files into one and read it from certain folder?

I am a newbie in matlab programming. I have a problem in coding to import multiple csv files into one from certain folder:
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.csv'); %gets all csv files in struct
That is the code. But it only shows the name of csv files, not read the content of the data. Could you please help me to find the code to read all the csv files into one file?
Thank you very much for your help

1 Comment

Did you finally solved this task of importing?
Could you please share your knowledge of how did you that before?

Sign in to comment.

 Accepted Answer

You can use tabularTextDatatstore to collect the CSV files and read them into one table
>> ds = tabularTextDatastore(filepath,'FileExtensions','.csv')
>> T = readall(ds)
Or if the data is too big, read "chunks"
while hasdata(ds)
T = read(ds);
end

3 Comments

Hi Jeremy, for filepath inside the function, I should change that to the csv directory I'm trying to import right? Do I need to put quotation mark for the path?
I'm trying to run this code but it gives me error.
ds = tabularTextDatastore(
'E:\\Matlab\Project1','FileExtensions','.csv','SelectedVariableNames',{'date','win','team','opposing_team'});
T = read(ds)
That command works great for me. I was able to import 98 csv files into one big table for my project.
In the command :
ds = tabularTextDatastore(filepath,'FileExtensions','.csv')
the parameter (filepath) was somthing like 'C:\project_data\imported_data_*'
Where the files name are : imported_data_01.csv to imported_data_99.csv
You can also look at all the list od the files importes by doing:
ds.Files

Sign in to comment.

More Answers (1)

You're only getting the list of files in the folder. You need to import them using csvread
For example,
fileNames = {myfiles.name};
for k = 1:numel(fileNames)
data{k} = csvread(fileNames{k});
%%do whatever you want
end
Handling with multiple files is discussed extensively already. Just look it up on this forum. Here are some obvious links.

5 Comments

I am sorry. I have no experience in coding with matlab so this is not easy for me. I have opened some question regarding my problem, but they already define the directory. In my case, the folder can be anything and I want to open the multiple csv files from that folder by using the codes, can it also work with your code above?
You don't have to be sorry. You have made efforts already. With the two lines you have written, you already got the list of csv files, now you just need to import them, i.e. store the data in some variables in the workspace and do the same thing for all files. With the imported data, you can then do whatever you want to do with it, plot,export as a single file, etc.
I am sorry I still did not understand. When I run my two line codes, I know that there are 47 csv files enlisted that I want to merge and read. For example in my case I continue my code with the codes you write:
myDir = uigetdir; %gets directory
myFiles = dir(fullfile(myDir,'*.csv'); %gets all csv files in struct
fileNames = {myfiles.name}; % (what should I modify here?)
for k = 1:47(fileNames)
data{k} = csvread(fileNames(k));
I am really sorry to ask you too much. I tried to find it since morning, and I tried the codes from internet by modifying a bit but I got error :(
Well, just trying out random things without knowing how it actually works will hardly be productive. It's not that hard.
fileNames = {myFiles.name}; %previously it was myfiles
my code was just an example! myFiles is the variable name you use in your two lines of code. ANd that line of code extract only the filenames from the myFiles struct.
Then I have a for loop to iterate through each file, import them and store it in a variable.
data{k} = csvread(fileNames{k});
The above line is the one that reads each csv file and stores the contents in a variable called data. Once the for loop is finished, all your data will be inside this variable called data. Later you can use this variable to create a new csv file, i.e, that contain all the "merged data".
just try and import 1 csv file first! Read the links I gave you.
I got an error :(. I have read the information you've given but I got something like the image I attached. I really have no idea about how to import this file :((((

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!