I never liked Embedded MATLAB, precisely because of situations similar to what I'm going to ask :) In my experience, converting to Embedded MATLAB often requires a lot more effort than just rewriting a thing in C (when you know C and the MATLAB API good enough.)
But, well, I think some things simply cannot be avoided.
Anyway, here is my problem. In Embedded MATLAB, as in R2010a, you cannot do this:
for ii = <some range> parameter = <some string> switch parameter case 'first_string' % do stuff case 'another_string_that''s_larger_than_first_string' % do other stuff end % ...continue here end
where <some string> modifies each iteration. This is because the length of the parameter string is variable, which means that this parameter definition goes beyond the Matlab built-in capabilities:
??? Inconsistent expression or empty matrix. This expression must be a constant because its value determines the size or class of some expression. The constant string limit applies only to includes lines, does not include numbers.
Changing switch to its more direct form
switch <some string> ... end
certainly doesn't help:
??? Expected a numeric value. Found a mxArray
Even limiting the string size to a known constant length does not help:
parameter = char_array(ii, 1:4); % <--- NOTE: length is 4 characters switch parameter ... end
but no luck:
??? Inconsistent expression or empty matrix. This expression must be a constant because its value determines the size or class of some expression. The constant string limit applies only to includes lines, does not include numbers.
I see two exits:
- Match all valid strings with some numeric representation and use the switch to numbers
- Use
strcmp(i) inside the huge if-elseif-elseif-...-else-end .
Both are equally ugly IMHO, with 2. maybe more ugly (you'll need another external function, strcmp ) ...
So, is there any elegant way out of this?