Main Content

使用 MATLAB 脚本创建图

此示例说明如何在 MATLAB® 函数或脚本中包含 Stateflow® API 命令。通过创建包含 API 命令的脚本,您可以避免执行重复的图创建步骤,使用单个命令即可重新创建相同的模型。有关详细信息,请参阅Overview of the Stateflow API

运行 MATLAB 函数

本页底部定义的函数 makeMyModel 可以创建基本的 Stateflow 图,您可以将其重用为应用程序的模板。

ch = makeMyModel;
view(ch)

Stateflow chart with a hierarchy of states. The outer state is called A. It contains two inner states called A1 and A2.

创建基本图函数

此函数创建一个 Stateflow 图,并返回对应的 Stateflow.Chart 对象。

function ch = makeMyModel

创建模型并访问新的 Stateflow.Chart 对象。

    rt = sfroot;
    prev_machines = find(rt,"-isa","Stateflow.Machine");
    sfnew;
    curr_machines = find(rt,"-isa","Stateflow.Machine");
    m = setdiff(curr_machines,prev_machines);
    ch = find(m,"-isa","Stateflow.Chart");

在图中创建状态 A

    sA = Stateflow.State(ch);
    sA.Name = "A";
    sA.Position = [50 50 310 200];

在状态 A 内创建状态 A1

    sA1 = Stateflow.State(ch);
    sA1.Name = "A1";
    sA1.Position = [80 120 90 60];

在状态 A 内创建状态 A2

    sA2 = Stateflow.State(ch);
    sA2.Name = "A2";
    sA2.Position = [240 120 90 60];

创建从 A1A2 的转移。

    tA1A2 = Stateflow.Transition(ch);
    tA1A2.Source = sA1;
    tA1A2.Destination = sA2;
    tA1A2.SourceOClock = 3;
    tA1A2.DestinationOClock = 9;

向状态 A 添加默认转移。

    dtA = Stateflow.Transition(ch);
    dtA.Destination = sA;
    dtA.DestinationOClock = 0;
    dtA.SourceEndPoint = dtA.DestinationEndpoint-[0 30];
    dtA.MidPoint = dtA.DestinationEndpoint-[0 15];

向状态 A1 添加默认转移。

    dtA1 = Stateflow.Transition(ch);
    dtA1.Destination = sA1;
    dtA1.DestinationOClock = 0;
    dtA1.SourceEndPoint = dtA1.DestinationEndpoint-[0 30];
    dtA1.MidPoint = dtA1.DestinationEndpoint-[0 15];
end

另请参阅

函数

对象

相关主题