Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to elegantly ignore some return values of a MATLAB function?
    primarykey
    data
    text
    <p>Is it possible to get the 'nth' return value from a function without having to create dummy variables for all <code>n-1</code> return values before it?</p> <p>Let's say, I have the following function in MATLAB:</p> <pre><code>function [a,b,c,d] = func() a = 1; b = 2; c = 3; d = 4; </code></pre> <p>Now suppose, I'm only interested in the <em>third</em> return value. This can be accomplished by creating one dummy variable:</p> <pre><code>[dummy, dummy, variableThatIWillUse, dummy] = func; clear dummy; </code></pre> <p>But I think this is kind of <em>ugly</em>. I would think that you might be able to do something like one of the following things, but you can't:</p> <p><code>[_, _, variableThatIWillUse, _] = func;</code></p> <p><code>[, , variableThatIWillUse, ] = func;</code></p> <p><code>variableThatIWillUse = func(3);</code></p> <p><code>variableThatIWillUse = func()(3);</code></p> <p>Are there any <em>elegant</em> ways to do this that do work?</p> <hr> <p>So far, the best solution is to simply use the <code>variableThatIWillUse</code> as a dummy variable. This saves me from having to create a real dummy variable that pollutes the work-space (or that I would need to clear). In short: the solution is to use the <code>variableThatIWillUse</code> for every return value up until the interesting one. Return values after can simply be ignored:</p> <pre><code>[variableThatIWillUse, variableThatIWillUse, variableThatIWillUse] = func; </code></pre> <p>I still think this is very ugly code, but if there is no better way, then I guess I'll accept the answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload