Azzera filtri
Azzera filtri

How to find all valid calling syntax's?

3 visualizzazioni (ultimi 30 giorni)
Andrew
Andrew il 18 Nov 2014
Commentato: Andrew il 19 Nov 2014
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?

Risposta accettata

Geoff Hayes
Geoff Hayes il 19 Nov 2014
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.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by