【问题标题】:How do I suppress lines of code inside one function when that function is being called from within another function?当从另一个函数中调用该函数时,如何抑制该函数中的代码行?
【发布时间】:2015-04-10 17:04:54
【问题描述】:

我有两个函数,一个是从另一个内部调用的。我希望第一个函数的某些部分在第二个函数中被调用时不执行。

function vvec = vecVelocity(varargin);
%must be preceded with a 'syms var real' declaration where var is
%the parameter of your vector function

if nargin > 1 & nargin < 3
   r = [sym(varargin(1:end))];
elseif nargin > 3 
    disp('too many inputs')
    return
else r = [sym(varargin(1))];
end

if length(r) < 3
    r = [r,0];
end

dr = diff(r);
uT = vecUnitTan(r);
speed = sqrt(sum(dr.^2));
v = speed*uT;
vvec = matlabFunction(v);
disp(['Simplified Symbolic Form: ' char(simplify(sym(vvec)))]);

当我从命令窗口调用以下第二个函数时,我想禁止显示最后一行 disp(...)

function speed = vecSpeed(r);
%must be preceded with a 'syms var real' declaration where var is
%the parameter of your vector function

v = sym(vecVelocity(r));
sp = sqrt(sum(v.^2));
speed = matlabFunction(sp);
disp(['Simplified Symbolic Form: ' char(simplify(sym(speed)))]);

目前,调用 vecSpeed 函数会导致从 vecSpeed 函数中调用的其他前面的函数(以及一些从 vecVelocity 函数中调用的函数)显示一堆语句,但我只想要disp(...) 来自要调用的 vecSpeed 函数的语句,而不是其他任何语句。

【问题讨论】:

    标签: matlab function suppress disp


    【解决方案1】:

    在第一个函数中,可以放一段代码

    if numel(dbstack) == 1
      % Your code block
    end
    

    防止它们被执行,除非该函数是直接从命令行窗口调用的。

    另一种方法是检查是否存在一些变量,例如debug_1debug_2 等。您可以在调用第一个函数时从第二个函数传递这些变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 2019-10-11
      • 2018-06-14
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多