フィルターのクリア

Store structure output into an array that you can index

2 ビュー (過去 30 日間)
Hayao
Hayao 2023 年 2 月 2 日
コメント済み: Hayao 2023 年 2 月 3 日
I used a [X,fval,exitflag,output] = fmincon(fun,...) for global optimization that is in a parfor loop.
In each loop, I get an "output" that show something like:
output =
struct with fields:
iterations: 24
funcCount: 84
constrviolation: 0
stepsize: 6.9162e-06
algorithm: 'interior-point'
firstorderopt: 2.4373e-08
cgiterations: 4
message: 'Local minimum found that satisfies the constraints....'
bestfeasible: [1x1 struct]
The information here is important so I want to store them in each loop. After the loop is done, I am going to use indexing to call for the output of a particular calculation that finished with an exitflag = 1.
X, fval, exitflag are easy to store for recalling them later since they are scalar numbers (using normal array). However, the output is a structure. I saw that the structure array could be used, but it seems to me that creating empty TotalNumberOfIteration-by-1 structure array is impossible without directly naming each of the elements.
I want to know of a way to store in a similar way to normal array to recall them later. Is there such way?

採用された回答

Luca Ferro
Luca Ferro 2023 年 2 月 2 日
i would either store it in a 1xn struct (n is the number of outputs you want to store):
for (your current loop condition, i'll call the index it idx)
[..,output(idx)] = fmincon(fun,..) %this stores your your outout in a 1xn struct
end
%to access the struct (let's say the first one)
output(1)
..
output(idx)
%to access the values
output(1).iterations
..
output(idx).iterations
%to access the struct within the values
output(1).bestfeasible.fieldnameinsidethestruct
or i would go for a cell array
for (your current loop condition, i'll call the index it idx)
[..,output{1}] = fmincon(fun,..) %this stores your your outout in a 1xn cell array
end
%to access the cell array (let's say the first one)
output{1}
..
output{idx}
%to access the values
output{1}.iterations
..
output{idx}.iterations
%to access the struct within the values
output{1}.bestfeasible.fieldnameinsidethestruct
The main difference is that the first method allows only structs with the same dimensions, the latter one is a bit more flexible.
  5 件のコメント
Luca Ferro
Luca Ferro 2023 年 2 月 2 日
編集済み: Luca Ferro 2023 年 2 月 2 日
thank you for expanding on the differences, i missed that for sure
Hayao
Hayao 2023 年 2 月 3 日
I see, I didn't know that was how I was supposed to do it. I did it wrong, which is why cell array didn't work for me.
Huge thanks guys. Now it worked!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by