Calling a function with 2 inputs using only a single input
이전 댓글 표시
I have a function with inputs a and b and when i tried to call the function with one input a and use the inbuilt function isempty(b) its showing error as not enough input arguments. How can I correct this.
function out = function_name(a,b)
if ~isempty(b)
b = 0.1;
end
채택된 답변
추가 답변 (2개)
Rik
2018년 10월 3일
You need to test for existence, not for being empty, as someone could still use an empty input.
function out=function_name(a,b)
if ~exist('b','var')
b=0.1;
elseif isempty(b)
error('b should not be empty')
end
out=a*b;
end
You can also use nargin to check the number of input arguments.
Govind Sankar Madhavan Pillai Ramachandran Nair
2018년 10월 4일
0 개 추천
댓글 수: 1
Rik
2018년 10월 4일
You're welcome. The best way to thank us is to accept the answer that works best and give other working answers a vote.
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!