How to import multiple csv files into one and read it from certain folder?
Show older comments
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
Anil Kumar
on 18 Feb 2019
Did you finally solved this task of importing?
Could you please share your knowledge of how did you that before?
Accepted Answer
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
Kasih Ditaningtyas Sari Pratiwi
on 20 Oct 2017
KL
on 20 Oct 2017
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.
Kasih Ditaningtyas Sari Pratiwi
on 20 Oct 2017
KL
on 20 Oct 2017
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.
Read aboout for loops here: https://www.mathworks.com/help/matlab/matlab_prog/loop-control-statements.html
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.
Kasih Ditaningtyas Sari Pratiwi
on 20 Oct 2017
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!