Function with mismatched inputs?
이전 댓글 표시
I am going over someone's code and in their main file they call a function as such:
[ar] = function(ar, data(i+1,1), data(i,1), data(i+1,2), data(i+1,4), dt);
However, in the function file there are many more expected inputs:
function [ theta1,time1 ] = function(t,y,u,theta0,x0,dt,V_x,V_w,V_v,V_theta,V_r,V_e)
I'm not completely sure if this code runs, but is this valid code?
댓글 수: 1
per isakson
2016년 8월 12일
편집: per isakson
2016년 8월 12일
Try
>> cssm(1)
Name Size Bytes Class Attributes
x1 1x1 8 double
>> cssm(1,2)
Name Size Bytes Class Attributes
x1 1x1 8 double
x2 1x1 8 double
>> cssm
>>
>> cssm([],2)
Name Size Bytes Class Attributes
x1 0x0 0 double
x2 1x1 8 double
where
function [y1,y2] = cssm(x1,x2,x3)
whos
end
채택된 답변
추가 답변 (1개)
Image Analyst
2016년 8월 13일
It's true that you don't have to supply all declared arguments, but if you don't supply them all, then inside the function you'll need to check nargin for that condition and assign default values for the missing input arguments if they will be needed somewhere in the function. Like if a windowSize is optional and if they don't pass it in you want to use a value of 3, then you can say
if nargin < 5 % (or wherever it falls in the arg list)
windowSize = 3; % Assign default value.
end
카테고리
도움말 센터 및 File Exchange에서 Argument Definitions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!