function overloading problem in matlab

조회 수: 12 (최근 30일)
zhang
zhang 2012년 7월 31일
댓글: Walter Roberson 2015년 5월 14일
Hi guys,
Does matlab support function overloading? Just like, F(x, y) F(x, y, z)
If not, is there any ways to do it?
Thanks,
Zhong
  댓글 수: 1
Kurt Patzik
Kurt Patzik 2015년 5월 14일
Hi,
overloading is possible. However, for simple task I always use nargin instead:
function [ ans ] = fctName( a, b, c )
if nargin < 2 % checks, if the number of input-parameters is less than 2
b = 1;
c = 0;
end
...
end
Best, Jonas

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

답변 (1개)

Daniel Shub
Daniel Shub 2012년 7월 31일
It is not clear what you are trying to do. I think you might be looking for ways to define a function with a variable number of input arguments. If that is the case
doc varargin
The source of many problems is that MATLAB will let you redefine any function you want. For example
clear = @()disp('Have fun undoing this (:');
will "overload" the builtin CLEAR function. You could also create an mfile named clear.m
function clear(varargin)
disp('Have fun undoing this (:');
end
which will similarly "overload" CLEAR whenever it is on the path.
The MATLAB OO system allows what I would call true overloading. The OO system choses the exact function that is called based on the name of the function and the class of the first argument.
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 5월 14일
Minor addition: the OO system does not take into account the number of arguments. Well, other than the possibility of having no arguments; I don't recall which class is used for that.

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

카테고리

Help CenterFile Exchange에서 Waveform Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by