Programming and Data Types | ![]() ![]() |
関数ハンドルのタイプ
functions
によって出力される情報は、関数ハンドルによって表わされる関数のタイプにより異なります。この節では、関数のタイプに応じて何が出力されるかを説明します。関数ハンドルのカテゴリは、以下の通りです。
以下は、多重定義されていないMATLAB組み込み関数あるいはM-ファイル関数のハンドルです。関数のタイプが決定されていない関数ハンドル (例、Javaメソッド、存在しない関数)も、このカテゴリにあてはまります。
function: function name type: 'simple' file: 'MATLAB built-in function' for built-ins path and name of the default M-file for nonbuilt-ins (there is no methods field)
組み込み関数の関数ハンドルについてfunctions
を使います。
functions(@ones) ans = function: 'ones' type: 'simple' file: 'MATLAB built-in function'
非組み込み関数の関数ハンドルについてfunctions
を使います。
functions(@fzero) ans = function: 'fzero' type: 'simple' file: 'matlabroot\toolbox\matlab\funfun\fzero.m'
以下は、異なるクラスに対して多重定義された実現であるMATLAB組み込み関数あるいはM-ファイル関数のハンドルです。
function: function name type: 'overloaded' file: 'MATLAB built-in function' for built-ins path and name of the default M-file for nonbuilt-ins methods: [1x1 struct]
組み込み関数の関数ハンドルについてfunctions
を使います。
functions(@display) ans = function: 'display' type: 'overloaded' file: 'MATLAB built-in function' methods: [1x1 struct]
非組み込み関数の関数ハンドルについてfunctions
を使います。
functions(@deblank) ans = function: 'deblank' type: 'overloaded' file: 'matlabroot\toolbox\matlab\strfun\deblank.m' methods: [1x1 struct]
以下は、MATLABクラスのオブジェクトを作成する関数のハンドルです。
function: function name type: 'constructor' file: path and name of the constructor M-file (there is no methods field)
コンストラクタ関数の関数ハンドルについてfunctions
を使います。
functions(@inline) ans = function: 'inline' type: 'constructor' file: 'matlabroot\toolbox\matlab\funfun\@inline\inline.m'
以下はMATLAB サブ関数のハンドルで、サブ関数とはそのM-ファイルの基本関数でのみ利用可能なM-ファイル内で定義される関数です。サブ関数ハンドルについてfunctions
を使うとき、出力構造体のfile
フィールドは、サブ関数が定義されるM-ファイル名とパスを含みます。
function: function name type: 'subfunction' file: path and name of the M-file defining the subfunction (there is no methods field)
以下のgetLocalHandle
M-ファイルは、基本関数とサブ関数subfunc
を定義します。
% -- File GETLOCALHANDLE.M -- function subhandle = getLocalHandle() subhandle = @subfunc; % return handle to subfunction function subfunc() disp 'running subfunc'
getLocalHandle
の呼び出しは、サブ関数の関数ハンドルを出力します。そのハンドルをfunctions
に渡すと、以下の情報を出力します。
fhandle = getLocalHandle; functions(fhandle) ans = function: 'subfunc' type: 'subfunction' file: '\home\user4\getLocalHandle.m'
以下は、MATLAB プライベート関数のハンドルで、プライベート関数とは、親ディレクトリ内の関数でのみ利用可能なprivate
サブディレクトリで定義される関数です。プライベート関数ハンドルについてfunctions
を使うとき、出力構造体のfile
フィールドは、関数を定義するprivate
サブディレクトリ内のM-ファイル名パスを含みます。
function: function name type: 'private' file: path and name of the M-file in \private (there is no methods field)
以下のgetPrivateHandle
関数は、privatefunc
というプライベート関数のハンドルを出力します。
% -- File GETPRIVATEHANDLE.M -- function privhandle = getPrivateHandle() privhandle = @privatefunc; % return handle to private function
以下の関数privatefunc
は、\private
サブディレクトリにあります。
% -- File \PRIVATE\PRIVATEFUNC.M -- function privatefunc() disp 'running privatefunc'
getPrivateHandle
の呼び出しは、\private
で定義された関数privatefunc
のハンドルを出力します。.そのハンドルをfunctions
に渡すと、以下の情報を出力します。
fhandle = getPrivateHandle; functions(fhandle) ans = function: 'privatefunc' type: 'private' file: '\home\user4\private\privatefunc.m'
![]() |
functionsコマンドで出力されるフィールド | 関数ハンドルの演算 | ![]() |