Varargout - How to realize function call

조회 수: 2 (최근 30일)
Dario Walter
Dario Walter 2019년 10월 21일
답변: Dario Walter 2019년 11월 1일
Hey guys,
let us assume the following function:
function [out1,out2,out3]=test(j,k)
out1= nargin;
out2 = j-k;
if out2 == 10
out3 = 5;
end
end
The function call [val1,val2,val3] = test(10,11) returns an error because c is obviously undefined. How do I make this code run so that out3 is returned when it exists?
Thanks for your help!
  댓글 수: 1
Guillaume
Guillaume 2019년 10월 21일
The function call [val1,val2,val3] = test(10,11)
will always require 3 outputs from the function, so test must always return at least 3 outputs for that call to be valid.
That's independent of whether or not the function only returns some outputs under some conditions.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 10월 21일
Simpler:
function [out1,out2,out3]=test(j,k)
out1= nargin;
out2 = j-k;
out3 = [];
...
if out2 == 10
out3 = 5;
end
end

추가 답변 (2개)

per isakson
per isakson 2019년 10월 21일
편집: per isakson 2019년 10월 21일
varargout exercise. Try something like this
function varargout = test( j, k )
out1= nargin;
out2 = j-k;
if out2 == 10
out3 = 5;
end
if exist( 'out3', 'var' ) == 1
varargout = { out1, out2, out3 };
else
varargout = { out1, out2, [] };
end
end
or
function varargout = test( j, k )
out1= nargin;
out2 = j-k;
if out2 == 10
out3 = 5;
else
out3 = [];
end
varargout = { out1, out2, out3 };
end

Dario Walter
Dario Walter 2019년 11월 1일
Thanks yor your help folks! I thought that a more sophisticated way exists than setting out = [ ]. Anyway, I am happy with that :)

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by