Read .csv files in the right order
Show older comments
Hello, i have a lot of .csv files do read (around 1500) and need to get a value from each. The problem is that the files appear out of the right order. My code:
list = dir('directory.csv');
numFiles = length(list);
for iFile = 1:numFiles
FileName =list(iFile).name;
Data(iFile).FileName = FileName;
end
for i=1:numFiles
A =dlmread(Data(i).FileName,',',[4 1 4 6]); B(i)=A(1,3);
end
plot(B)
The list structure fills as appear in the picture in attachment.
How can i order the right way?
Accepted Answer
More Answers (1)
Akira Agata
on 17 Jul 2018
Edited: Akira Agata
on 17 Jul 2018
Please try the following before the for-loop.
list = dir(fullfile(yourDirectory,'*.csv'));
[~, idx] = sort(str2double(regexp({list.name},'\d+(?=.csv)','match','once')));
list = list(idx);
2 Comments
Tiago Tavares
on 17 Jul 2018
Akira Agata
on 17 Jul 2018
Seems strange... But, anyway, it's nice to hear that you now have a solution!
Categories
Find more on Time Series Events 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!