save column values of different multiple .txt files without overwriting in a for loop.

1 回表示 (過去 30 日間)
yr = [1951:2018]';
for j = 1:size(A)
fil = importdata([folder, A(j).name]);
file = dlmread(fullfile(folder , A(j).name));
for i = 1:size(yr,1)
a = find(file(:,1)==yr(i));
M(i,1) = sum(file(a,4));
end
end
I imported all files using 'dir' ansd stored in A. The problem is that Only Last file is getting operated on.
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 18 日
Are you want to write M(i,1) data in txt file?
Angana Borah
Angana Borah 2019 年 9 月 18 日
No. I want to bring the 4th column of all the files in the sructure to one data_array,M. Did you get my question? In the for loop, only the file file is getting read and sum of the column of only that file is calculated. Whereas, I want 4th column from all the files.

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

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 9 月 18 日
I want to bring the 4th column of all the files in the sructure to one data_array,
  1. Read the txt file one by one (As you mentioned you have done it sucessfully)
  2. iter i=1
  3. Extract the 4th column from i file
  4. Save the column data in cell array
  5. Repeat, iter 2
Step 3:
col_data{i}=writetable(column_data,['file_test',num2str(i),'.txt'])
  1 件のコメント
Angana Borah
Angana Borah 2019 年 9 月 19 日
Thanks a lot. But I do not want to store column data from various files in different .txt files but want to input in the different columns of the same matrix if that is possible.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by