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

7 views (last 30 days)
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 Comments
Guillaume Dupré
Guillaume Dupré on 23 Feb 2024
Edited: Guillaume Dupré on 23 Feb 2024
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 on 23 Feb 2024
"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:

Sign in to comment.

Accepted Answer

Guillaume Dupré
Guillaume Dupré on 23 Feb 2024
Edited: Guillaume Dupré on 23 Feb 2024
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 !

More Answers (1)

Matt J
Matt J on 23 Feb 2024
Edited: Matt J on 23 Feb 2024
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 Comment
Guillaume Dupré
Guillaume Dupré on 23 Feb 2024
Edited: Guillaume Dupré on 23 Feb 2024
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.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!