我有一个可以通过更改函数名称来解决的问题。但我想知道是否可以调用与我的用户定义函数同名的MATLAB定义函数。默认情况下,MATLAB始终使用用户定义的函数,但是我想在同一个脚本中使用两者。任何的想法?
MATLABfuzzytoolbox :: addrule(); userDefined :: addrule()
在使用addrule函数将其阴影之前,获取原始函数的句柄:
fuzzy_addrule = @addrule;
Run Code Online (Sandbox Code Playgroud)
该语句中的定义是“冻结”,在这个意义上,如果你以后重新定义或影子addrule是不会影响 fuzzy_addrule。
现在addrule,您可以定义函数,该函数将隐藏原始函数addrule,但不会fuzzy_rule。
addrule = @(x,y) x+y; %// very simple example
Run Code Online (Sandbox Code Playgroud)
因此,要使用您的函数,您只需编写:
>> addrule(3,4)
ans =
7 %// your function's result
Run Code Online (Sandbox Code Playgroud)
要使用原始功能,请致电fuzzy_addrule:
>> fuzzy_addrule(readfis('tipper'),[]) %// example call for fuzzy/addrule function
ans =
name: 'tipper'
type: 'mamdani'
andMethod: 'min'
orMethod: 'max'
defuzzMethod: 'centroid'
impMethod: 'min'
aggMethod: 'max'
input: [1x2 struct]
output: [1x1 struct]
rule: [1x3 struct]
Run Code Online (Sandbox Code Playgroud)
上面的要求在定义函数之前先创建工具箱函数的句柄。如果要在定义函数后访问工具箱函数,可以按以下步骤进行:
fuzzy_addrule。由于工具箱功能现在可见,因此句柄引用该功能。fuzzy_addrule工具箱函数的句柄。码:
curdir = pwd; %// take note of current folder
t = which('addrule', '-all'); %// t{1} is your function, t{2} is the toolbox function
fuzdir = regexp(t{2},'.+\\','match'); %// get only folder part
cd(fuzdir{1}); %// change to that folder
fuzzy_addrule = @addrule; %// define function handle
cd(curdir); %// restore folder
Run Code Online (Sandbox Code Playgroud)
完成此操作后,可以如上所述调用每个函数。