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

조회 수: 8 (최근 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