Readmatrix in struct using parfor

5 ビュー (過去 30 日間)
Konvictus177
Konvictus177 2022 年 6 月 27 日
コメント済み: Konvictus177 2022 年 6 月 30 日
Hi,
My for-loop that reads multiple .csv files (1 file per iteration) takes too much time. Therefore, I would like to increase the performance using parfor.
Using parfor and structs seems not to be straightforward. How can I adjust the following code to use a parfor?
Thanks.
% load the data from each file
% parfor i = 1:length(Files2Read)
for i = 1:length(Files2Read)
% create fieldname for each file to read
fld = ['File#', num2str(i) ];
% load data
Data.(fld) = readmatrix(Files2Read{i});
% store additional information:
Data.General.FieldName{i} = fld;
end
  1 件のコメント
Stephen23
Stephen23 2022 年 6 月 27 日
" How can I adjust the following code to use a parfor?"
Use actual indexing into a structure array rather than forcing a pseudo-index into the fieldname.

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

採用された回答

Jan
Jan 2022 年 6 月 27 日
編集済み: Jan 2022 年 6 月 27 日
Fields names cannot contain the charcater '#' .
fld = sprintf('File_%d', i);
Data.(fld) = readmatrix(Files2Read{i});
This should work.
AS Stephen told already, this would be nicer:
Data(i).Value = readmatrix(Files2Read{i});
or
Data.File(i) = readmatrix(Files2Read{i}); % Or File{i}
By the way, if the disk access is the bottleneck, parfor cannot help. Please post, if you code runs faster than with for.
  3 件のコメント
Jan
Jan 2022 年 6 月 28 日
Starting the parallel pool takes some time, but remember, that reading the files might store their contents in a RAM cache also. Then optimizing the code for repeated reading has much less effects in real applications.
Konvictus177
Konvictus177 2022 年 6 月 30 日
Thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by