Generate Code for transforming structure array to cell array

4 visualizzazioni (ultimi 30 giorni)
I have a structure array that I want to convert to a cell array. I can do this in MATLAB, but this function is part of a larger code base that I want to use in generated code. I'm struggling to get the cells extracted from the structure array into a cell array in such a way that is accetable for code generation. I've provided below the function that performs the transformation that I want in MATLAB, and a script that uses the function and tries to codegen it. I keep getting errors. Can someone help me generate code for this function or help me do the conversion in a better way that will work with MATLAB Coder? I'm new to MATLAB Coder, so I'm hoping this is something simple that I'm missing. Thanks!
Running the code in the question dialogue gives the wrong error. The corret error is: Directly accessing a field of a nonscalar struct is not supported for code generation. This limitation also applies to a property of a nonscalar object.
% This script calls the cellTform function and tries to generate code
% build the input struct array
StructIn(1).t = {magic(3)};
StructIn(2).t = {magic(3)};
StructIn(3).t = {magic(3)};
StructIn(4).t = {magic(3)};
StructIn(5).t = {magic(3)};
StructIn(6).t = {magic(3)};
% build the sorting index array
SortingInd = [1,3,2,5];
cellOut = cellTform(StructIn,SortingInd)
cellOut = 1x4 cell array
{3x3 double} {3x3 double} {3x3 double} {3x3 double}
ARGS{1} = coder.typeof(StructIn,[Inf 1],[true false]);
ARGS{2} = coder.typeof(SortingInd,[Inf 1],[true false]);
codegen cellTform -args ARGS
CODEGEN requires a MATLAB Coder license. Code generation failed: To view the report, open('codegen/mex/cellTform/html/report.mldatx')
Error using codegen
function cellOut = cellTform(arrayStructIn,SortingIndexIn)
%#codegen
% This function accepts an array of strcuts that contain a {1x1} cell of
% [3x3] doubles and an array if indices that need to be extracted from the
% array of structs and put into the output cell of
% {1xlength(SortingIndexIn)}.
% Inputs:
% arrayStructIn = array of structs that contain a {1x1} cell of [3x3]
% doubles = arrayStructIn(N).t{1}[3x3]
% SortingIndexIn = column vector in indices that must be extracted from
% arrayStructIn [Mx1], not sorted and output cell must retain this order
% Output:
% cellOut = cell array {1xM} of the extracted cells of arrayStructIn
tdum = horzcat(arrayStructIn(:).t); % make a copy of the input structure
% cellOut = horzcat({tdum{SortingIndexIn}}); % extract the cells I want
% another way...
Nsort = length(SortingIndexIn);
cellOut = cell(1,Nsort);
for cnt = 1:Nsort
cellOut{cnt} = tdum{SortingIndexIn(cnt)};
end
end
  1 Commento
Christopher Smith
Christopher Smith il 1 Mar 2025
It led to the following code which successfully worked through codegen. I'd still like to see if anyone has any insight into better ways to do this though. Thanks!
% This script calls the cellTform function and tries to generate code
% build the input struct array
StructIn(1).t = {magic(3)};
StructIn(2).t = {magic(3)};
StructIn(3).t = {magic(3)};
StructIn(4).t = {magic(3)};
StructIn(5).t = {magic(3)};
StructIn(6).t = {magic(3)};
% build the sorting index array
SortingInd = [1;3;2;5];
tic
cellOut = cellTform(StructIn,SortingInd)
toc
ARGS{1} = coder.typeof(StructIn,[1 Inf],[false true]);
ARGS{2} = coder.typeof(SortingInd,[Inf 1],[true false]);
codegen cellTform -args ARGS
tic
cellOutMex = cellTform_mex(StructIn,SortingInd)
toc
function cellOut = cellTform(arrayStructIn,SortingIndexIn)
%#codegen
% This function accepts an array of strcuts that contain a {1x1} cell of
% [3x3] doubles and an array if indices that need to be extracted from the
% array of structs and put into the output cell of
% {1xlength(SortingIndexIn)}.
% Inputs:
% arrayStructIn = array of structs that contain a {1x1} cell of [3x3]
% doubles = arrayStructIn(N).t{1}[3x3]
% SortingIndexIn = column vector in indices that must be extracted from
% arrayStructIn [Mx1], not sorted and output cell must retain this order
% Output:
% cellOut = cell array {1xM} of the extracted cells of arrayStructIn
% first part - transform the struct array
NArrayStruct = size(arrayStructIn,2);
celldum = cell(1,NArrayStruct);
for cnt = 1:NArrayStruct
celldum{cnt} = arrayStructIn(cnt).t{:};
end
% seconde part - extract the cells I want
Nsort = length(SortingIndexIn);
cellOut = cell(1,Nsort);
for cnt = 1:Nsort
cellOut{cnt} = celldum{SortingIndexIn(cnt)};
end
end

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su MATLAB Coder in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by