title: How to use a switch construct with variable-length strings in Embedded Matlab?
body: <p>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).</p>
  
  <p>But oh well, I guess some things just can't be avoided. </p>
  
  <p>Anyway, here's my problem. In Embedded MATLAB, as it is in R2010a, you can't do this: </p>
  
  <pre><code>for ii = &lt;some range&gt;
  
      parameter = &lt;some string&gt; 
  
      switch parameter
  
          case 'first_string'
              % do stuff
  
          case 'another_string_that''s_larger_than_first_string'
              % do other stuff
      end
  
      % ...continue here
  
  end
  </code></pre>
  
  <p>where <code>&lt;some string&gt;</code> changes every iteration. This is because the length of the string <code>parameter</code> is variable, meaning that this definition of <code>parameter</code> is beyond Embedded Matlab's capabilities: </p>
  
  <blockquote>
    <p>??? 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.</p>
  </blockquote>
  
  <p>Changing the <code>switch</code> to its more direct form </p>
  
  <pre><code>switch &lt;some string&gt; 
      ...
  end
  </code></pre>
  
  <p>doesn't help of course: </p>
  
  <pre><code>??? Expected a numeric value. Found a mxArray
  </code></pre>
  
  <p>Even constraining the size of the string to a known constant length does not help: </p>
  
  <pre><code>parameter = char_array(ii, 1:4);  % &lt;--- NOTE: length is 4 characters
  
  switch parameter
      ...
  end
  </code></pre>
  
  <p>but no luck: </p>
  
  <blockquote>
    <p>??? 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.</p>
  </blockquote>
  
  <p>I see two ways out: </p>
  
  <ol>
  <li>Map all permissible strings to some numeric representation, and use a switch on the numbers</li>
  <li>Use <code>strcmp(i)</code> inside a huge <code>if-elseif-elseif-...-else-end</code> construct. </li>
  </ol>
  
  <p>Both are equally ugly IMHO, with 2. perhaps being uglier (you'd need another extrinsic function, <code>strcmp</code>)...</p>
  
  <p>So, is there any <em>elegant</em> way out of this? </p>