如何编写具有可变输出信号大小的S函数?

时间:2013-07-17 15:02:56

标签: matlab simulink

我想写一个块,它从所选目录发送所有图像文件。

图像尺寸不同,因此输出信号尺寸应有所不同。

不幸的是,我无法找到改变每一步信号大小的方法。这里有许多未记录的功能,例如

block.OutputPort(1).DimensionsMode = 'Variable';

block.OutputPort(1).CurrentDimensions = [1 block.InputPort(1).Data];

等等。我无法推断出正确操作所有这些东西的方法......

更新

例如,这个S函数

function Test_SF_01(block)
% Level-2 MATLAB file S-Function.

    setup(block);

function setup(block)

    % Register number of ports and parameters
    block.NumInputPorts  = 0;
    block.NumOutputPorts = 1;
    block.NumDialogPrms  = 0;

    % Setup functional port properties to dynamically inherited
    block.SetPreCompOutPortInfoToDynamic;

    % Register the properties of the output port
    block.OutputPort(1).DimensionsMode = 'Variable';
    block.OutputPort(1).SamplingMode   = 'Sample';

    % Register sample times
    %  [-1, 0] : Inherited sample time
    block.SampleTimes = [-1 0];

    % Register methods called at run-time
    block.RegBlockMethod('Outputs', @Outputs);

function Outputs(block)
    block.OutputPort(1).CurrentDimensions =  floor(rand(1,2)*10)+1;

导致错误

  

输出端口1的无效变量维分配   'Test_01 / Level-2 MATLAB S-Function'。可变尺寸的数量   然而,MATLAB数组的长度是2

为什么?

2 个答案:

答案 0 :(得分:2)

以下S函数生成可变尺寸信号。他们的关键问题是Dimensions属性的初始集定义了维度的MAXIMAL值,这绝对不是从文档中清楚的,而错误消息通常是无关紧要的。

function Test_SF_01(block)
% Level-2 MATLAB file S-Function.

    setup(block);

function setup(block)

    % Register number of ports and parameters
    block.NumInputPorts  = 0;
    block.NumOutputPorts = 1;
    block.NumDialogPrms  = 0;

    % Setup functional port properties to dynamically inherited
    block.SetPreCompOutPortInfoToDynamic;

    % Register the properties of the output port
    block.OutputPort(1).DimensionsMode = 'Variable';
    block.OutputPort(1).Dimensions = [10000 10000];

    block.OutputPort(1).SamplingMode   = 'Sample';

    % Register sample times
    %  [-1, 0] : Inherited sample time
    block.SampleTimes = [-1 0];

    % Register methods called at run-time
    block.RegBlockMethod('Outputs', @Outputs);

function Outputs(block)

     dims = floor(rand(1,2)*10)+1;
     block.OutputPort(1).CurrentDimensions = dims;

     data = rand(dims);
    block.OutputPort(1).Data = data;

答案 1 :(得分:0)

您是否也从doc查看了以下代码示例?见Operations for Variable-Size Signals。除了代码中的内容之外,还有更多内容。可能最好从基本模板msfuntmpl_basic.m开始,就像在文档中一样。

function setup(block)
% Register the properties of the output port
block.OutputPort(1).DimensionsMode = 'Variable';
block.RegBlockMethod('SetInputPortDimensionsMode',  @SetInputDimsMode);

function DoPostPropSetup(block)
%Register dependency rules to update current output size of output port a depending on
%input ports b and c
block.AddOutputDimsDependencyRules(a, [b c], @setOutputVarDims);

%Configure output port b to have the same dimensions as input port a
block.InputPortSameDimsAsOutputPort(a,b);

%Configure DWork a to have its size reset when input size changes.
block.DWorkRequireResetForSignalSize(a,true);

function SetInputDimsMode(block, port, dm)
% Set dimension mode
block.InputPort(port).DimensionsMode = dm;
block.OutputPort(port).DimensionsMode = dm;

function setOutputVarDims(block, opIdx, inputIdx)
% Set current (run-time) dimensions of the output
outDimsAfterReset = block.InputPort(inputIdx(1)).CurrentDimensions;
block.OutputPort(opIdx).CurrentDimensions = outDimsAfterReset;