(MATLAB) Ignoring output arguments

  • Context: MATLAB 
  • Thread starter Thread starter juef
  • Start date Start date
  • Tags Tags
    Matlab Output
Click For Summary

Discussion Overview

The discussion centers around handling multiple output arguments in MATLAB functions, specifically how to ignore certain outputs when they are not needed. Participants explore various methods to avoid unnecessary variable assignments and memory usage.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant expresses frustration with MATLAB's syntax checker flagging unused output variables when only one output is needed from functions like ode45.
  • Another participant suggests that it might be possible to select a specific component of a returned vector directly, though this does not address multiple output arguments.
  • A different participant clarifies that their concern is about selecting specific outputs from functions with multiple return values, noting that their current method of using dummy variables feels inelegant.
  • One suggestion is made to edit the original function to remove unnecessary output arguments if that is a priority.
  • A participant mentions that newer versions of MATLAB allow the use of a tilde (~) to ignore output variables, providing a cleaner syntax for ignoring unwanted outputs.
  • Another participant expresses appreciation for the information about the tilde syntax.
  • One participant proposes changing the order of output variables to prioritize the needed output, which may help in some cases.

Areas of Agreement / Disagreement

Participants have differing views on the best approach to ignore unwanted output arguments, with some suggesting editing functions, while others prefer using the tilde syntax. No consensus is reached on a single best method.

Contextual Notes

Limitations include the dependency on MATLAB version for the tilde syntax and the potential need for function modifications which may not be feasible in all situations.

Who May Find This Useful

Users of MATLAB who work with functions that return multiple outputs and are looking for ways to manage or ignore unwanted outputs efficiently.

juef
Messages
27
Reaction score
0
Hey all,

Suppose I have a MATLAB function that returns two (or more) output arguments, but I only care about the second one, and I do not wish to assign the value of the first one to a variable.

What bugs me is when, for example, I solve an ODE:

[t, x] = ode45(blablabla)

and then MATLAB editor's automatic syntax checker tells me that the variable 't' is never used. Of course it isn't, because I don't need it :P But more importantly, it uses some memory unnessarily. So, is there any way to totally ignore that argument?

Thank you all!
 
Physics news on Phys.org
I haven't used MATLAB in ages, but if a function returns a vector is it not possible to pick off one component like:

x=ode45(blablabla)[2]
 
I have been wondering exactly the same thing. Haven't found a answer, though.

Just to be clear, it is not about selecting a value of a returned vector, but selecting a specific output. It is especially annoying if I have a multiple output arguments.

function [A,B,C,D,E,F] = myFun(inputarg)
...​
end

If I only want the last output argument F I usually just do the following
[dummy, dummy, dummy, dummy, dummy, F] = myFun(input)

but its ugly.
 
edit the code or save as the original function without the extra arguments if it is really important to you.
 
Newer versions of Matlab let you use a ~ in place of the output variables you choose to ignore. So instead of

[dummy, dummy, dummy, dummy, dummy, F] = myFun(input),

you could put

[~, ~, ~, ~, ~, F] = myFun(input)

See this blog post for more info.
 
Wow, that is handy. Thanks for the link.
 
Simply change the position of the variables
[x, t] = ode45(blablabla)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K