필터 지우기
필터 지우기

Tilde doesn't work for ignoring my inputs

조회 수: 24 (최근 30일)
Patrick Aoun
Patrick Aoun 2016년 6월 6일
댓글: Walter Roberson 2020년 9월 27일
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
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
Jessica Avellaneda
Jessica Avellaneda 2020년 9월 27일
What can I use to ignore inputs arguments when I call the function?
Walter Roberson
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개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by