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 CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by