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 Kommentar

per isakson
per isakson am 12 Aug. 2016
Bearbeitet: per isakson am 12 Aug. 2016
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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Aug. 2016

0 Stimmen

Yes, declaring extra input arguments is fine, as long as the code does not use any argument that is not passed. The nargin() call helps determine how many arguments were actually passed, and you can use exist() to determine whether an individual parameter was passed.

3 Kommentare

per isakson
per isakson am 12 Aug. 2016
"declaring extra input arguments is fine" &nbsp However, I fail to find this explicitly stated in the documentation.
Tip When you define a function with multiple input or output arguments, list any required arguments first. This allows you to call your function without specifying optional arguments.
This example shows how to determine how many input or output arguments your function receives using nargin and nargout.
per isakson
per isakson am 12 Aug. 2016
Yes, that might be the best information on "extra input" in the documentation, but I wouldn't say it's explicit.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 13 Aug. 2016

0 Stimmen

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

Kategorien

Mehr zu Argument Definitions finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by