How to automatically create struct-fields from a cell array

2 ビュー (過去 30 日間)
Moritz Pohl
Moritz Pohl 2018 年 2 月 14 日
編集済み: Moritz Pohl 2018 年 2 月 17 日
Hello folks,
it's late and I'm not able to figure out how this works. I use an optimizer which passes some parameter values to my mathematical model. However, the model reads all parameters out of struct-fields and I have to transform the double vector before the model can actually the values. Currently, I do this transformation manually:
function [p] = MakeStruct(parameter)
p.parameter1 = parameter(1);
.
.
p.parameter34 = parameter(34);
end
which of course is suboptimal because when I change a parameter or its name I have to go through the code and change it in several places. Does anybody know, how I can make this automatically?
When I create the parameters, my script also returns a 1x34 cell array with all parameter names. So far, I tried to combine both the cell array with all names and the double array with all values with "cell2struct" but it didn't worked for me.
Cheers

採用された回答

Moritz Pohl
Moritz Pohl 2018 年 2 月 17 日
編集済み: Moritz Pohl 2018 年 2 月 17 日
Figured it out on my own. For anyone who's interested:
% conversion of to double arrays to a 1x1 struct with fieldnames from 'parameter_names' and fieldvalues from 'parameter_values'
parameter_values = [2 3]; % double array
parameter_cell = num2cell(parameter); % conversion of double array to cell array
parameter_names = {'qmax', 'kS'}; % cell array
p = cell2struct(parameter_cell, parameter_names , 2); % conversion of both cell arrays to 1x1 struct

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by