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
Thank you! I'll start using [ ] with isempty.
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.
@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.
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?
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!
"[...] 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.
What can I use to ignore inputs arguments when I call the function?
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
