Dynamic size table with MATLAB coder

8 views (last 30 days)
Diogo Tecelão
Diogo Tecelão on 29 Nov 2020
Hi all,
I'm intend to convert my feature extraction code into C using MATLAB coder, but I'm facing trouble when trying to aggregate the features in a table. I've created a reproductible code which raises my error:
function t = table_concat_test
t = table();
for i=1:10
features = struct();
features.a = 1;
features.b = 2;
features.c = 3;
features.d = 4;
extra_info = struct();
extra_info.name = "name";
extra_info.age = 19;
t = [t; struct2table(features) struct2table(extra_info)];
end
end
When I run the compile comamnd:
codegen table_concat_test
I get the following error:
??? Code generation does not support table arrays.
I was able to solve this error with the following changes:
t_temp = [t; struct2table(features) struct2table(extra_info)];
t = t_temp;
But now I get another different error:
??? The value of non-tunable property 'rowDim.length' does not match.
Any idea how I can do this? Thanks!

Answers (1)

Siddharth Bhutiya
Siddharth Bhutiya on 9 Dec 2020
The error seems to suggest that you are assigning conflicting values to your table variable 't'. This might be happening because you are reusing the same variable name 't' to mean different tables during each iteration. Something like this would be the obvious thing to do in MATLAB but, since you are generating C code, things are slightly different there. One way to make this work would be to create a cell array of the sub tables and then concatenate them in one call after the loop.
function t = table_concat_test
temp = cell(1,10);
for i=1:10
features = struct();
features.a = 1;
features.b = 2;
features.c = 3;
features.d = 4;
extra_info = struct();
extra_info.name = {'name'};
extra_info.age = 19;
temp{i} = [struct2table(features) struct2table(extra_info)];
end
t = vertcat(temp{:});
end
>> codegen table_concat_test
Code generation successful.
>> table_concat_test_mex
ans =
10×6 table
a b c d name age
_ _ _ _ ________ ___
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
1 2 3 4 {'name'} 19
Note that I had to change extra_info.name to a cellstr instead of string, since MATLAB unfortunately does not support code generation for string arrays at this point.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!