(MATLAB) Ignoring output arguments

In summary, the conversation discusses how to handle situations where a MATLAB function returns multiple output arguments, but the user only needs one of them. The issue of unnecessary memory usage is also brought up. Suggestions for handling this include using a dummy variable or the ~ symbol to ignore the unwanted output argument, or changing the position of the variables to match the desired output.
  • #1
juef
27
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
  • #2
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]
 
  • #3
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.
 
  • #4
edit the code or save as the original function without the extra arguments if it is really important to you.
 
  • #5
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.
 
  • #6
Wow, that is handy. Thanks for the link.
 
  • #7
Simply change the position of the variables
[x, t] = ode45(blablabla)
 

1. What is the purpose of ignoring output arguments in MATLAB?

Ignoring output arguments in MATLAB allows you to perform a function or operation without storing the output in a variable. This is useful when the output is not needed for further calculations or analysis.

2. How do I ignore output arguments in MATLAB?

To ignore output arguments in MATLAB, use the tilde symbol (~) in place of a variable name in the output argument list. This indicates to MATLAB that you do not want to store the output in a variable.

3. Can I ignore specific output arguments in MATLAB?

Yes, you can ignore specific output arguments in MATLAB by using the tilde symbol (~) for the specific argument you want to ignore. The other output arguments will still be stored in their respective variables.

4. What happens if I don't ignore output arguments in MATLAB?

If you do not ignore output arguments in MATLAB, the output will be stored in the specified variables. If you do not assign enough variables for all the output arguments, MATLAB will display an error.

5. Are there any drawbacks to ignoring output arguments in MATLAB?

One potential drawback is that ignoring output arguments can make your code less readable and may make it harder for others to understand. Additionally, if you need the output for further calculations, you will have to re-run the function or operation. It is important to carefully consider whether ignoring output arguments is the best approach for your specific situation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
951
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top