How do I save multiple results from a function as different structures in a for loop?

3 ビュー (過去 30 日間)
jj
jj 2013 年 4 月 30 日
Hi all,
I've a function that returns multiple results in a structure (i.e results.m1, results.m2). As running this function each time is computationally intensive with many simulations within it, I am attempting to put this in a parallel computing environment, as follows:
parfor z = 1:8
results=function(x,y,z);
end
My question then is: How can I get the function to return me 8 different structures containing my results? I can't seem to get it to do so.
Would love to have some help here.
Thanks :)

回答 (1 件)

Matt J
Matt J 2013 年 4 月 30 日
編集済み: Matt J 2013 年 4 月 30 日
Why not create a struct array as follows?
parfor z = 1:8
results(z)=function(x,y,z);
end
  2 件のコメント
jj
jj 2013 年 4 月 30 日
I actually tried that. It gives me an error telling me "The expression to the left of the equals sign is not a valid target for assignment". Any idea why this might be the case?
Matt J
Matt J 2013 年 4 月 30 日
編集済み: Matt J 2013 年 4 月 30 日
Whatever it is, it's not coming from code you've shown us. Try running the simplified example below as an mfile. You should find that it works fine.
function test
parfor z = 1:8
results(z)=makestruct(z);
end
results
function S=makestruct(z)
S.a=z;
S.b=z;

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by