Best practice for saving only one output from a vector-outputting function

What's the proper way to request only one variable from a vector outputting function, i.e.
[WantThis DontWantThis] = myfun(x, y, ... )
In the past I've just saved the DontWantThis output as a variable called trash and not made reference to it again. This seems rather unprofessional. Is there a cleaner way?

 採用された回答

Cedric
Cedric 2013 年 4 月 14 日
編集済み: Cedric 2013 年 4 月 14 日
Simply
WantThis = myfun(x, y, ... )
if you just need/want the first output. More generally
[a, b, c, d] = myfun(x, y, ... )
even if there could be more output args ( e, f, g, ..).
If don't want to name args that you don't need, which are preceding args that you do need, you can use ~ as a placeholder:
[~, ~, c, ~, e] = myfun(x, y, ... )
Note that ~ is supported since 2009 if I remember well (edit: ref).

1 件のコメント

Rylan
Rylan 2013 年 4 月 14 日
It was the placeholder tilde I was looking for. Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2013 年 4 月 14 日
You can replace it with a tilde (~)
[WantThis ~] = myfun(x, y, ... )
or just leave it off entirely
WantThis = myfun(x, y, ... )
In the above case, DontWantThis will be thrown away, essentially not returned. I think most people use this latter way, though it can only be used for ignoring return arguments to the right. In other words if you just put one return value:
theOutput = myfun(x, y, ... )
theOutput will be WantThis, not DonotWantThis. Understand?

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2013 年 4 月 14 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by