Clear Filters
Clear Filters

MATLAB Coder Runtime error: Table size input must be constant

1 view (last 30 days)
I have the following code snippet where I am trying to output a table with single element per cell from a table of multiple variable cell elements. This is:
NumVars = width(tablein);
varlen = size(tablein(1,1), 2);
varType = repmat({class(tablein{1,1}(1))},NumVars,1);
varNames = tablein.Properties.VariableNames;
tablesize = [varlen NumVars];
tableout = table('Size',tablesize,'VariableTypes',varType,'VariableNames',varNames);
for i = 1:NumVars
len = length(tablein{1,i});
transval = tablein{1,i}.';
if len < varlen
extraval = mean(tablein{1,i}(end-1:end));
transval(varlen) = extraval;
end
tableout.(i) = transval;
end
And I am getting the following error:
% When preallocating a table, the table size input must be constant.
I've tried using the functions length, size, width but they don't seem to work around this error. I reckon it might be due to the fact that the functions need to have their parameters, such as dimension, explicitly passed for code generation, but in this case it seems I am stuck. Any ideas?

Answers (1)

Tom Harwood
Tom Harwood on 27 Nov 2019
Hello Vasileios,
The diagnostic you encountered comes from the table class' constructor; for codegen, the table class requires the size argument be constant. I reached out to the table developers and received a suggestion for a workaround:
"The question is: why is tablesize not constant at compile time?
tablesize is [size(tablein(1,1), 2) width(tablein)].
Is tablein(1,1) fixed-sized or variable-sized? size(tablein(1,1), 2) needs to be constant in this case.
The width of tablein should always be fixed. But in R2019b, there is an issue that the width function can return non-constant output even when the table has fixed width.
If it is the latter problem, the customer can try to work around it. For example, instead of calling width(tablein), convert the table to cell array first:
size(tablein{:,:},2)"
The expression transval = tablein{1,i}.'; will also be problematic; code generation does not support transposition of cell arrays. Following this example, I believe this expression will work around this for your case:
transval = reshape(tablein{1,i}, [1 size(tablein{1,i})]);

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!