필터 지우기
필터 지우기

How to find all valid calling syntax's?

조회 수: 3 (최근 30일)
Andrew
Andrew 2014년 11월 18일
댓글: Andrew 2014년 11월 19일
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.
  댓글 수: 1
Andrew
Andrew 2014년 11월 19일
That answered it perfectly. Thanks a lot!

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

추가 답변 (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