How to accumulate values ​​from time 1 to the last time

1 ビュー (過去 30 日間)
pink flower
pink flower 2021 年 9 月 23 日
編集済み: Stephen 2021 年 9 月 26 日
I have more than 10000 files and I want to accumulate the values of given variable between all these times and then plot it on a single image. How can I do it? The files are 1000x1000 matrices.
  2 件のコメント
pink flower
pink flower 2021 年 9 月 26 日
This must be my output (M3):
M1 = magic(3);
M2 = magic(3)+17;
M3 = M1 + M2;

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

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 9 月 23 日
Here is one of the possible routines to read all 1e4 files and collect all data into a signle variable and the rest is relatively simple:
clearvars
FILE = fullfile('C:\Users\...', '*.txt'); % Directory where the files are residing. Your data file extension, e.g.: .txt, .xlsx, .csv, etc
LIST = dir(FILE);
N = length(LIST); % N = 1e4 as you stated that 1e4 files
D = zeros(1e3, 1e3, N);
for ii = 1 : N
FFName = fullfile(LIST(ii).folder, LIST(ii).name);
DATA = readmatrix(FFName);
D(:, :, ii) = DATA; % NOW D contains all data from 10000 files
end
... % Select and process the part of D that is necessary
  2 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 9 月 26 日
These are not coding problems but rather memory problems. That means either you should process your data chunk by chunk or increase the memory in your matlab settings.

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


Stephen
Stephen 2021 年 9 月 26 日
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P, '*.txt')); % Select the file extension to suit your data files
M = 0;
for k = 1:numel(S)
F = fullfile(P, S(k).name);
M = M + readmatrix(F);
end
  6 件のコメント
Stephen
Stephen 2021 年 9 月 26 日
To ignore NaN when summing you can use SUM with the appropriate flag, e.g.:
M = zeros(1000,1000);
for k = 1:numel(S)
F = fullfile(P, S(k).name);
N = whateverCodeYouUseToImportYourFileData;
M = sum(cat(3,M,N),3,'omitnan');
end

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

Community Treasure Hunt

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

Start Hunting!

Translated by