simulink 定义一个长度不确定的数组

请问simulink怎么定义一个长度可变的数组,并用switch函数输出这个数组
.

simulink啥版本的?我这里写了一个小例子,把输入向量前1~3个元素复制到输出,输入>3设为输入向量
看看这里:https://ww2.mathworks.cn/help/simulink/ug/what-is-variable-size-data.html

function y = myFunction(u)
N = length(u);
y = zeros(N,1);
switch N
    case 1
        y(1) = u(1);
    case 2
        y(1) = u(1);
        y(2) = u(2);
    case 3
        y(1) = u(1);
        y(2) = u(2);
        y(3) = u(3);
    otherwise
        y = u;
end


和x下降的非线性关系,利用switch模块实现该功能。下面给出相应的simulink模型图和代码。

Simulink模型图:

Simulink模型图

代码:

function [sys,x0,str,ts] = hysteresis(t,x,u,flag)
% HYSTERESIS S-function implementing hysteresis curve with switch
% (C) 2019-2021 Baidu, Inc.
if flag==0

    % define input and output ports
    sizes = simsizes;
    sizes.NumContStates  = 0;
    sizes.NumDiscStates  = 0;
    sizes.NumOutputs     = 1;
    sizes.NumInputs      = 1;
    sizes.DirFeedthrough = 1;
    sizes.NumSampleTimes = 1;

    % set the sample time in seconds [0.1 0] means sample time 0.1 seconds
    ts = [0.1 0]; % fixed in this example

    sys = simsizes(sizes);
    x0  = [];
    str = [];
elseif flag==2
    % define switch points
    x2up = 0.6;
    x2down = -0.6;

    % define output value at switch points
    y2up = 0.8;
    y2down = -0.8;

    % calculate output
    if u>x2up
        y = y2up;
    elseif u<x2down
        y = y2down;
    else
        y = u;
    end

    sys = y;

else
    sys = [];
end

解释:在该模型中,输入信号为x,输出信号为y。模型中使用了两个switch模块,x>0.6时,输出y=0.8;x<-0.6时,输出y=-0.8;在-0.6<=x<=0.6的范围内,输出y=x。

以上模型可以实现简单的滞回特性曲线输出。在更复杂的情况下,也可以利用类似的方式定义可变长度的数组,并使用switch实现需要的功能。