Read multiple .CSV files with csvread using a loop

10 views (last 30 days)
Dear All,
I hope that you may help me,
Say I have n files named "droplet.n.csv" where n goes from 0 to 100 ( I am attaching five .csv files)
Each file has two rows. I want to extract the second row of each file and save it in another file. So what I would end up having on MATLAB is a matrix with many rows as n there are (in this case 100)
Thanks in advance

Accepted Answer

Stephen23
Stephen23 on 10 Feb 2021
Edited: Stephen23 on 10 Feb 2021
V = 0:4;
N = numel(V);
C = cell(1,N);
for k = 1:N
F = sprintf('droplet.%d.csv',V(k));
C{k} = readmatrix(F,'NumHeaderLines',1);
end
M = vertcat(C{:})
M = 5×14
0 0 0 -0.0000 -0.0000 -0.0000 0.0000 0.0708 0 0 0 0.0130 0.0115 0.0117 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0202 0.0000 -0.0000 0.0000 0.0130 0.0115 0.0117 0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0035 0.0000 0.0000 0.0000 0.0152 0.0115 0.0128 0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0118 0.0000 -0.0000 0.0000 0.0167 0.0115 0.0132 0.0000 -0.0000 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0130 0.0000 -0.0000 0.0000 0.0177 0.0115 0.0134
Or use readtable to get a table at the end, which might be more convenient to use.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!