I never liked Embedded MATLAB much, precisely because of situations like what I'm about to ask :) In my experience, conversion to Embedded MATLAB often takes a lot more effort than simply re-writing the thing in C (when you know C and the MATLAB API well enough).
But oh well, I guess some things just can't be avoided.
Anyway, here's my problem. In Embedded MATLAB, as it is in R2010a, you can't 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>
changes every iteration. This is because the length of the string parameter
is variable, meaning that this definition of parameter
is beyond Embedded Matlab's capabilities:
??? Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression. The limitation to constant strings applies only for switches on strings, not switches on numbers.
Changing the switch
to its more direct form
switch <some string>
...
end
doesn't help of course:
??? Expected a numeric value. Found a mxArray
Even constraining the size of the string 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:
??? Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression. The limitation to constant strings applies only for switches on strings, not switches on numbers.
I see two ways out:
- Map all permissible strings to some numeric representation, and use a switch on the numbers
- Use
strcmp(i)
inside a hugeif-elseif-elseif-...-else-end
construct.
Both are equally ugly IMHO, with 2. perhaps being uglier (you'd need another extrinsic function, strcmp
)...
So, is there any elegant way out of this?