0

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

图像大小不同,因此输出信号大小应有所不同。

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

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

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

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

更新

例如,这个 S-Function

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;

导致错误

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

为什么?

4

2 回答 2

2

以下 S-Function 生成可变维度信号。他们的关键问题是初始Dimensions属性集定义了维度的最大值,这在文档中绝对不清楚,而错误消息大多无关紧要。

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;
于 2013-07-17T21:17:03.530 回答
0

您是否还从文档中查看了以下代码示例?请参阅可变大小信号的操作。那里的内容比代码中的内容要多。可能最好从基本模板开始,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;
于 2013-07-17T15:35:38.023 回答