Matlab函数 - 显示双精度而不是参数名称

时间:2015-03-11 23:12:33

标签: matlab

我已经定义了一个关于其他变量的Matlab函数。但是,当我打印出函数的一般形式时,它不会计算出函数的值 其他参数。我怎样才能显示他们的价值而不是他们的名字?

下面给出了一个最小的例子:

t_br = 0.0199;
Gsrv = @(s) 1/(t_br*s + 1);
disp(Gsrv) % don't want to display 't_br' but 0.0199.

谢谢,

3 个答案:

答案 0 :(得分:2)

使用subs

Gsrv = @(s) 1/(t_br*s + 1);
subs(Gsrv,t_br,0.0199) % don't want to display 't_br' but 0.0199.

或只是

t_br =0.0199
Gsrv = @(s) 1/(t_br*s + 1);
subs(Gsrv) % don't want to display 't_br' but 0.0199.

答案 1 :(得分:0)

您可以获取函数句柄的工作空间,该句柄存储该函数所需的所有变量。例如,

t_br = 0.0199;
Gsrv = @(s) 1/(t_br*s + 1);
f = functions(Gsrv)
f.workspace{:}

这并不像显示值那样容易。

答案 2 :(得分:0)

一般情况,其中该函数可能具有任意数量的参数

d = 5; %// desired number of digits
f = functions(Gsrv); %// function
fString = f.function; %// string defining the function
split = regexp(fString, '@\(.+?\)', 'end');
fString1 = fString(1:split); %// @(...) part
fString2 = fString(split+1:end); %// remaining part, to be passed to `subs`
fParam = f.workspace{1}; %// parameters of function
fParamNames = fields(fParam); %// names of parameters of function
fParamValues = struct2cell(fParam); %// values of parameters of function
result = [fString1 ' ' char(vpa(subs(fString2, fParamNames, fParamValues), d))]
    %// pass fString2 to `subs` to do the parameter substitutions
    %// apply vpa to use decimal representation with the desired number of digits
    %// convert that to string (with `char`), and  concatenate with fString1

示例:

a1 = 1;
b_2 = 2.3333333;
cc = -10;
Gsrv = @(x,y1,z_2) x^a1 + b_2*y1 + exp(cc*z_2);

产生

result =
@(x,y1,z_2) x + 2.3333*y1 + exp(-10.0*z_2)