(MATLAB) Ignoring output arguments

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

This discussion addresses how to ignore unused output arguments in MATLAB functions, particularly when using the ode45 function. Users often encounter warnings about unused variables when they only need specific outputs. The solution involves using the tilde (~) operator to suppress unwanted outputs, allowing for cleaner code. This feature is available in newer versions of MATLAB, enhancing memory efficiency and code readability.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of function output arguments in MATLAB
  • Knowledge of the ode45 function for solving ordinary differential equations
  • Basic coding practices for memory management in MATLAB
NEXT STEPS
  • Explore the use of the tilde operator in MATLAB for ignoring output arguments
  • Learn about MATLAB function definitions and multiple output arguments
  • Investigate best practices for optimizing memory usage in MATLAB
  • Review the latest features in MATLAB updates related to function outputs
USEFUL FOR

MATLAB developers, data scientists, and engineers who frequently work with functions returning multiple outputs and seek to streamline their code and improve performance.

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
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K