How to create struct from fieldnames and values cell arrays for code generation ?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 23 de Feb. de 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:
Más respuestas (1)
Matt J
el 23 de Feb. de 2024
Editada: Matt J
el 23 de Feb. de 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(:)})
1 comentario
Ver también
Categorías
Más información sobre Simulink Environment Customization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!