How to use switch construction with variable-length strings in Embedded Matlab?

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?

+4
source share
1 answer

Unfortunately, I do not know of a method other than the alternatives that you mention.

To make matters worse, MATLAB does not have a built-in char array to double the scalar hash function. Therefore, if you want to match strings with numbers, you need to provide a custom hash function.

Personally, I would go with my second method. It is ugly but readable.

Since you are saying that you only need to consider the first 4 characters of each line, another option for matching strings to numbers is an explicit search:

 function y = fcn(u) %#codegen y = u; cases = [ '1111' '2222' '3333' ]; for i = 1 : 3 switch i case 1 p = '1111a'; case 2 p = '2222bb'; otherwise p = '3333ccc'; end for j = 1 : size(cases, 1) if isequal(cases(j, 1 : 4), p) switch j case 1 y = y + 1; case 2 y = y + 2; case 3 y = y + 3; end end end end end 

In my opinion, this is not very readable and probably spends a lot of time if you have a large number of lines. You can think of something more perfect, but I still consider it a hack.

+1
source

Source: https://habr.com/ru/post/1485765/


All Articles