フィルターのクリア

How to create struct from fieldnames and values cell arrays for code generation ?

3 ビュー (過去 30 日間)
Hi,
I am trying to make a "code generation friendly" function:
[output_struct] = declareStruct(list_fields, list_values)
that creates a structure knowing its fieldnames and their associated values.
The goal is to use this function in Simulink, through a Matlab system block, simulated using code generation.
None of the approches described in How to create a struct from a cell array of fieldnames and a cell array of values? - MATLAB Answers - MATLAB Central (mathworks.com) work since code generation does not support cell2struct or transpose for cell arrays.
Is there a way to create a struct this way in MATLAB R2021b, or is it only possible to explicitely declare my struct in a hardcoded fashion ?
Thanks !
Guillaume.
  3 件のコメント
Guillaume Dupré
Guillaume Dupré 2024 年 2 月 23 日
編集済み: Guillaume Dupré 2024 年 2 月 23 日
The values of my fields do not have the same size.
Here is an example of what I would like to do:
list_fields = {'a'; 'b'; 'c'; 'd'}; % the function fieldnames() returns a column vector in my case
list_values = {1; [2,3,4]; 5; 6};
args = [list_fields'; list_values'];
output_struct = struct(args{:});
which returns:
struct('a', 1, ...
'b', [2 3 4], ...
'c', 5, ...
'd', 6);
However, I cannot create the args variable in a code generation context.
This is needed here to respect the syntax struct(field1, value1, field2, value2, etc...).
Stephen23
Stephen23 2024 年 2 月 23 日
"I cannot create the args variable in a code generation context."
Perhaps one of these would work:
  • RESHAPE()
  • create two new cells arrays with the required size, transfer the content:

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

採用された回答

Guillaume Dupré
Guillaume Dupré 2024 年 2 月 23 日
編集済み: Guillaume Dupré 2024 年 2 月 23 日
Hi all,
Based on Stephen23 and Matt J advices, I managed to produce the following solution:
list_fields = {'a'; 'b'; 'c'; 'd'}; % the function fieldnames() returns a column vector in my case
list_values = {1; [2,3,4]; 5; 6};
% For loop to cast list_fields and list_values in another cell without
% transpose or concatenate
num_fields = length(list_fields);
args = cell(2,num_fields);
for i = 1:num_fields
args{1,i} = list_fields{i};
args{2,i} = list_values{i};
end
output_struct = struct(args{:})
Thank you for your help !

その他の回答 (1 件)

Matt J
Matt J 2024 年 2 月 23 日
編集済み: Matt J 2024 年 2 月 23 日
Does it support transpose for normal arrays? If so, then you might be able to do,
list_fields = {'a'; 'b'; 'c'; 'd'}; % the function fieldnames() returns a column vector in my case
list_values = {1; [2,3,4]; 5; 6};
args = [list_fields, list_values];
idx=reshape(1:numel(args),size(args))';
output_struct = struct(args{idx(:)})
output_struct = struct with fields:
a: 1 b: [2 3 4] c: 5 d: 6
  1 件のコメント
Guillaume Dupré
Guillaume Dupré 2024 年 2 月 23 日
編集済み: Guillaume Dupré 2024 年 2 月 23 日
Thank you for your answer.
I tried it but unfortunately code generation does not support concatenation of cell arrays.
I will try to combine it with the 2nd solution of Stephen23 to see if this works.

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

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by