Switch with many string cases won't compile in code generation
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a function .m to be called in a simulink model and generate C code with Simulink coder.
In this function I take an array, that's actually a string converted to uint8, and do a switch with LOTS OF cases.
function out = fcn(stringIn)
%#codegen
[index]=findFirstZero_fcn(stringIn); %find the first zero in the string
localString = char(stringIn(1:index));
if index > 0
switch localString
case 'string_firstLength'
out = something;
case 'string_secondLength'
out = something_else;
%...
% 140 MORE CASES !!!!! %
%...
case 'string_140thLength'
out = some_other_thing;
otherwise
out = some_different_thing;
end
end
Strings in the cases are not all the same length.
Running the simulation is perfectly working. When I generate code of the model and compile it, I get a stack error. In fact the generated code turns in an insanely nested sequence of if-else and do-while, that looks not compilable.
The problem is not solved either if I reduce the switch to if-elseif-else or to a series of
if strcmp(localString, 'string_firstLength')
%set output
return
end
if strcmp(localString, 'string_secondLength')
%set output
return
end
%etc etc
In this case the generated C code is still too nested :
if strcmp(localString, 'string_firstLength')
{
out = something;
} else {
if strcmp(localString, 'string_firstLength')
{
out = something_else;
} else {
if ......
}
}
}
}
The situation does not change either selecting or unchecking the Code Generation configuration option "Reduce if-elseif-else to switch-case".
Any suggestion? Thanks.
0 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre Simulink Coder en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!