Tilde doesn't work for ignoring my inputs
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a function
function [output] = myfunction(a,b,c)
Which can run without any inputs because I coded "default" inputs using if ~exist. I.e. I can type myfunction in the command box and it will run.
However, when I try myfunction(~,~,c) for instance, it says that "Using ~ to ignore a value is not permitted in this context."
Any ideas?
Thanks.
PS: I know about vargin. I'd rather avoid it if possible.
採用された回答
Steven Lord
2016 年 6 月 6 日
You can use tilde to ignore input arguments when you define the function.
You cannot use tilde to ignore input arguments when you call the function.
You cannot use tilde to ignore output arguments when you define the function.
You can use tilde to ignore output arguments when you call the function.
If you want to be able to specify only the third argument, there must be some way for your function to disambiguate the one-input call from a call that specifies only the third input. Usually in MathWorks function this is done by specifying [] for the arguments for which the user doesn't want to specify. In that case the code would use the default value if either nargin is too small or if nargin is large enough and the specified input argument isempty.
9 件のコメント
Patrick Aoun
2016 年 6 月 6 日
Thank you! I'll start using [ ] with isempty.
Pranav Gupta
2020 年 5 月 16 日
Keyword arguments in Python is the name of the game.
Python is way way better than Matlab. I deeply regret having started scientific computing with Matlab. I hope Cleve Moler reads this comment.
If the Matlab admin is so insecure as to delete this comment for its content, feel free to do so.
Patrick Aoun
2020 年 5 月 16 日
@Pranav
Yea I share that regret. I've actually just started learning python a month ago. I wish I knew that as clearly back when I posted that question.
Walter Roberson
2020 年 5 月 17 日
I hope Cleve Moler reads this comment.
For what purpose? Are you hoping that he will decide that MATLAB was a mistake, and close down Mathworks?
Pranav Gupta
2020 年 6 月 5 日
No, so that he rehauls MATLAB and makes it better than Python in most respects. If MATLAB was free, Cleve would have been my deity.
I don't understand how to deal with a paid software that isn't clearly better than open source software. I feel that MATLAB today is like an unfair booby trap for most non-CS people in STEM fields, and Python isn't as popular among them as it should be.
But I am sure there are cases where MATLAB is better. The issue is that most people don't have to deal with such cases.
20 years ago when Python wasn't the king, MATLAB was indispensable. But today, even a 5 year old will correctly conclude that MATLAB needs an overhaul.
Make MATLAB great again!
Patrick Aoun
2020 年 6 月 5 日
"[...] unfair booby trap for most non-CS people in STEM fields"
that's precisely what happened to me
Shrug. I have been around long enough to have hacked VMS. Now that had a meaningful keyword processing facilities. The kind of keyword processing that I see people complain about being missing in MATLAB is, by comparison, minor "syntactic sugar" against name/value pairs.
Really, what is the difference between writing
plot(x, y, 'MarkerShape', 'o')
compared to
plot(x, y, MarkerShape='o')
other than the second of those having two fewer apostrophes ?
If you were talking about something that made an actual difference with what you could do with the language, then I would have more patience. For example, python "slices" are an interesting language feature that is in some ways more "pure" compared to the MATLAB closest equivalents, and that can make a difference in what can actually be done. But I cannot seem to locate any enthusiasm for getting upset about the syntax for keyword processing in MATLAB. Not unless it starts getting back to what you could do with VMS.
Jessica Avellaneda
2020 年 9 月 27 日
What can I use to ignore inputs arguments when I call the function?
Walter Roberson
2020 年 9 月 27 日
You cannot ignore input arguments when you all the function.
If the function itself is ignoring that argument, then you can pass anything of any datatype, but you have to pass something (even if empty)
If the function itself is not ignoring that argument, then it needs to receive something (unless it is a trailing argument and the function is using nargin to detect whether trailing arguments were passed in). The convention for Mathworks functions is that [] is passed to hold a place to permit a later meaningful argument to be passed.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
