How to find all valid calling syntax's?

3 次查看(过去 30 天)
I've got a function that I need to find all the valid calling syntax's for. The line after function is : [ a ] = drag_model( system, velocity, altitude ). System is a struct and velocity and altitude are scalars. I've got another one: [value, terminal, direction] = events(t,y,system,m_events) t is a scalar, y is a vector, system and m_events are structs. Would there be more calling syntax's for this one because it has more outputs, or does that not matter?

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-19
Andrew - determining the different ways to call a function (calling syntax) can depend on how the function is written. For example, does your first function allow for the input parameters to be optional? If this function is written as
function [a] = drag_model(system, velocity, altitude)
if ~exist('altitude','var')
altitude = 100;
elseif ~exist('velocity','var')
velocity = 25;
end
% other code
then the above assigns default values to the velocity and altitude inputs, allowing the function to be called in one of three ways
drag_model(systemVar);
drag_model(systemVar,velocityVar);
drag_model(systemVar,velocityVar,altitudeVar);
where *Var are just the variables being passed into the function. Note that if you have not coded your function to allow for optional parameters, then the above calls would generate errors. Since no output parameter is included in the above calls, the a output will be returned but be unassigned (or rather, it will be assigned to the ans local variable).
Now if we do include the output parameter (so that a is assigned to something other than ans), then we have three more ways we can call this function
a = drag_model(systemVar);
a = drag_model(systemVar,velocityVar);
a = drag_model(systemVar,velocityVar,altitudeVar);
For your second function, events, you need not supply all output parameters, so it could be called as
events(t,y,system,m_events); % first output parameter assigned to ans
[value] = events(t,y,system,m_events);
[value, terminal] = events(t,y,system,m_events);
[value, terminal, direction] = events(t,y,system,m_events);
Again, depending upon how you have coded the function, the inputs can be optional (or not). Try executing the various combinations of your functions to get an idea of what works and what doesn't.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by