title: How can I count the number of properties in a structure in MATLAB?
body: <p>I have a function that returns one or more variables, but as it changes (depending on whether the function is successful or not), the following does NOT work:</p>
  
  <pre><code>[resultA, resultB, resultC, resultD, resultE, resultF] = func(somevars);
  </code></pre>
  
  <p>This will sometimes return an error, <em>varargout{2} not defined</em>, since only the first variable <code>resultA</code> is actually given a value when the function fails. Instead I put all the output in one variable:</p>
  
  <pre><code>output = func(somevars);
  </code></pre>
  
  <p>However, the variables are defined as properties of a struct, meaning I have to access them with <code>output.A</code>. This is not a problem in itself, but I need to count the number of properties to determine if I got the proper result.</p>
  
  <p>I tried <code>length(output)</code>, <code>numel(output)</code> and <code>size(output)</code> to no avail, so if anyone has a clever way of doing this I would be very grateful.</p>