【问题标题】:How to write S-function with variable size of output signal?如何编写输出信号大小可变的 S-Function?
【发布时间】:2013-07-17 15:02:56
【问题描述】:

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

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

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

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;

导致错误

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

为什么?

【问题讨论】:

标签: matlab simulink


【解决方案1】:

以下 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;

【讨论】:

    【解决方案2】:

    您是否还查看过文档中的以下代码示例?见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;
    

    【讨论】:

    • 我需要没有输入端口,只有输出。你能改写我的方式吗?
    • 您的示例中在哪里定义了abcSetInputPortDimensionsModeAddOutputDimsDependencyRules 方法记录在哪里?我看不到 SetOutputPortDimensionsMode 方法。
    • 我认为a,b,cd是在基本模板msfuntmpl_basic.m中定义的,相信你可以通过在命令行输入sfundemos来访问.其余的请参阅:mathworks.co.uk/help/simulink/sfg/…。看起来AddOutputDimsDependencyRules 没有记录在案,你想问问 MathWorks。我认为您对SetOutputPortDimensionsMode 的看法是正确的。
    • 这个帖子也是我有用的:mathworks.co.uk/matlabcentral/answers/…
    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多