Read .csv files in the right order

3 ビュー (過去 30 日間)
Tiago Tavares
Tiago Tavares 2018 年 7 月 16 日
編集済み: Stephen23 2021 年 4 月 18 日
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?

採用された回答

Stephen23
Stephen23 2018 年 7 月 17 日
編集済み: Stephen23 2021 年 4 月 18 日
The simplest solution is to download my FEX submission natsortfiles and use it like this:
D = 'C:\directory';
S = dir(fullfile(D,'*.csv'));
S = natsortfiles(S); % alphanumeric sort by filename
for k = 1:numel(N)
F = fullfile(D,S(k).name)
... your code
end
  1 件のコメント
Tiago Tavares
Tiago Tavares 2018 年 7 月 17 日
Hello, thank for your response.
I had tried the funtion you mencioned prior but it didn't work. But now its all ok!! It gave me the cell array in the right order.
Thank you very much!!!

サインインしてコメントする。

その他の回答 (1 件)

Akira Agata
Akira Agata 2018 年 7 月 17 日
編集済み: Akira Agata 2018 年 7 月 17 日
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 件のコメント
Tiago Tavares
Tiago Tavares 2018 年 7 月 17 日
Hello! Thank you but it did not worked. It gave an error on the regexp. Meanwhile i tried the natsortfiles funtion from the other answer and it gave me what i wanted. Thank you for the answer!
Akira Agata
Akira Agata 2018 年 7 月 17 日
Seems strange... But, anyway, it's nice to hear that you now have a solution!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by