1

I have a problem that many others had before, so I read some discussions and references before asking the following:

I have a Matlab function block in Simulink, which would like to be a modulator. It gets as input a [nx1] frame of data and should return a an [lx1] frame, where either l=n or l = n/K, for some K which divides n.

However, the (very simplyfied extract of) code

function ak = Modulator(dataFrame,dataType)

      coder.varsize('ak',length(dataFrame));

      M = 4; % this is for simplicity in this extract

      if dataType == 1 %input is a binary stream, bit mapping required
              ak = zeros(round(length(dataFrame)/log2(M)),1);
      else % input data is a stream of integer
              ak = zeros(length(dataFrame),1);
      end  

  end

Doesn't compile because

"Data 'ak' is inferred as a variable size matrix,
 while its specified type is something else."

Now, in line 2 I specified that it is a variable size matrix, and I also used an if/else constructor to initialize it.

To make the compiler happy, one may check the "Variable number of columns" checkbox for ak, in the Data and Ports Manager, but this turns out in a new error, because the blocks in cascade don't accept variable data, at least the ones I need, like digital filters.

4

2 回答 2

1

由于两个问题,我上面给出的代码没有编译:

  • ak 的初始化取决于 dataType 的值,它不是输入,而是在 MATLAB 功能块上方的掩码中定义的参数。但是,它被标记为“可调”,因此 if/else 的两个分支之一的选择是在运行时进行的。这使得 ak 在运行时成为可变大小的数组,因此出现错误。

  • 要理解第二个错误,请考虑我提供的示例代码的更完整版本,它过于简化(对此我深表歉意)

    函数 ak = Modulator(dataFrame,modscheme,dataType)

    persistent constellation
    persistent M
    if isempty(constellation) || isempty(M)
        switch modcode
            case 1
                constellation = get_it_from_some_function;
                M = xx;
            case n
                ...
        end
    
    if dataType == 1 %input is a binary stream, bit mapping required
              ak = zeros(round(length(dataFrame)/log2(M)),1); % <- ERROR!!
    else % input data is a stream of integer
              ak = zeros(length(dataFrame),1);
    end      
    end
    

此代码无法编译,因为正如编译器所见,M 可能会在运行时发生变化,因此 ak 的大小也会发生变化。实际上这永远不会发生,因为 M 只分配了一次,但编译器不接受它

于 2013-06-17T20:54:06.403 回答
1

首先,您应该查看是否希望您的数据是可变大小的。根据示例代码,您似乎希望大小根据数据类型而有所不同。dataType 感觉就像一个编译时(而不是运行时)变量。如果是这种情况,您应该在 MATLAB Function 模块中将此作为参数并确保它不可调整。然后,您可以使用 set_param 函数设置此参数的值。如果你这样做,那么 dataType 的值在编译时是已知的,并且只有一个“if”分支被分析,这将导致固定大小的“ak”。

如果不是这种情况并且您想在运行时切换数据类型,那么您必须检查数据和端口管理器中的可变大小选项。在这种情况下,您的 ak 是可变大小的数据。此输出只能与可以支持可变大小输入的其他块一起使用。

于 2013-06-17T18:14:50.443 回答