| 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コマンドで出力されるフィールド | 関数ハンドルの演算 |