To convert a variable (struct with fields of numeric matrix) to a sliced variable (an array of struct)

4 ビュー (過去 30 日間)
I was doing code refactor to apply parfor to some legacy code. Legacy code creates a big struct variable called 'Boot', with fields of numeric matrix.
I want to convert it to a new sliced variable "BootS" , but if I do the conversion like the attached image, Matlab give me warnings that I should preallocate it.
( iters means number of iterations.)
warnings.png
I tried to preallocate BootS as the code commented out , it fails with runtime errors.
Anyone has good idea to convert it elegantly??
  2 件のコメント
Edric Ellis
Edric Ellis 2019 年 2 月 21 日
There are a few possible failure modes here - please could you post a self-contained example that fails when using parfor. I tried the following in R2018b, and it worked fine, so I'm missing something:
iters = 10;
clear BootS;
BootS(iters) = struct();
parfor i = 1:iters
BootS(i).PD.sim = rand();
BootS(i).PDOS.sim = rand();
end
(I also presume you're doing some computationally intensive work in the parfor loop - simply reformatting data in this way using parfor is highly unlikely to gain you any performance advantage)
Owen Zhang
Owen Zhang 2019 年 2 月 25 日
thank you, i got it.. there is no runtime error with your code.
(in this loop, i just want to convert the data from Boot to Boots, a new sliced variable. Boot is not a sliced variable , can't be used in parfor.)
but with runtime error by creating a function returning the struct..
but anyway , i see how to do it now.
iters = 10;
clear Boots;
Boots(iters) = struct();
for i=1:iters
Boots(i) = genOneStruct();
end

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

採用された回答

Owen Zhang
Owen Zhang 2019 年 2 月 25 日
closed
see answer above

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by