【问题标题】:I got error message about simulink "Output argument is not assigned on some execution paths"我收到有关 simulink 的错误消息“未在某些执行路径上分配输出参数”
【发布时间】:2014-09-10 08:34:21
【问题描述】:

在 simulink 中,我使用“MATLAB 函数”块制作了一些模型 但我在这里遇到了错误消息。

这里是代码和错误信息。

function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)

if mode == 1
     VTAS = initialVTAS + (a * t) ;
     postVTAS = VTAS;

 elseif mode == 2
     datasize = length(preVTAS);
     lastvalue = preVTAS(datasize);
     VTAS = lastvalue + 0;
     postVTAS = VTAS;

 end

 end

在某些执行路径上未分配输出参数“VTAS”。

函数 'MATLAB 函数' (#36.25.28),第 1 行,第 26 列:

“fcn”

启动诊断报告。

我认为输出“VTAS”没有问题

请教我什么是问题。

【问题讨论】:

    标签: matlab simulink


    【解决方案1】:

    正如编译器告诉您的,在某些情况下没有分配给 VTAS 的输出值。原因是您只在模式为 1 或 2 时为该输出分配值。编译器不知道哪些值对mode 是可行的。要解决此问题,您需要确保在任何情况下都分配了 VTAS。

    这可以通过例如添加一个 else 结构,如下所示:

    function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
    
    if mode == 1
        VTAS = initialVTAS + (a * t) ;
        postVTAS = VTAS;
    
    elseif mode == 2
        datasize = length(preVTAS);
        lastvalue = preVTAS(datasize);
        VTAS = lastvalue + 0;
        postVTAS = VTAS;
    
    else
        VTAS     = NaN;
        postVTAS = NaN;
    end
    end
    

    编辑:

    此外,else 情况抛出错误是一种很好的做法。这对调试很有帮助。

    作为一个小提示,对于每种情况,postVTAS 等于 VTAS,因此从函数中返回两者基本上是多余的。

    【讨论】:

      猜你喜欢
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 1970-01-01
      相关资源
      最近更新 更多