How to modify this could so it can read from different files groups?

1 回表示 (過去 30 日間)
Ismail Qeshta
Ismail Qeshta 2019 年 2 月 20 日
コメント済み: Ismail Qeshta 2019 年 2 月 21 日
Hi,
I have this code that reads from 10 files each maximum X value and its corresponding Y value and prints all results in one file (named "Result_1, Result_2,...")
Now, I have 400 files. I need to modify the code, so it can read the different groups sequentially and print the output file for each group.
For instance, I need it to read from 1 to 10, then from 11 to 20, then from 21 to 30, and so on until 391 to 400.
Hence, the printed output files are: Result_1 (for files 1 to 10), Result_2 (for files 11 to 20), and so on until the output file Result_40 (for files 391 to 400).
The code:
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
Z = nan(N,1);
for k = 1:N
data = load(C{k});
[~,idx] = max(data(:,1));
Z(k) = data(idx,2);
end
dlmwrite('Result_1.txt',Z)

採用された回答

Bob Thompson
Bob Thompson 2019 年 2 月 20 日
Add a second loop. Instead of just going through all files in a single loop, have an outer loop that looks for the number of groups you have, and an inner loop that has the number of files in each group. The example below assumes you have a constant number of files in each group.
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
g = 10;
Z = nan(g,1);
for k = 1:N/g
for j = 1:g;
data = load(C{10*(k-1)+j});
[~,idx] = max(data(:,1));
Z(j) = data(idx,2);
end
dlmwrite(['Result_',num2str(k),'.txt'],Z);
end
  10 件のコメント
Bob Thompson
Bob Thompson 2019 年 2 月 20 日
g refers to the number of sets of data to be printed into each output. So, I think we are saying the same thing, yes.
Ismail Qeshta
Ismail Qeshta 2019 年 2 月 21 日
Thank you very much Bob for all your time and help.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by