How to add data in Stateflow by using the API ?

15 visualizaciones (últimos 30 días)
I am doing SIL testing and would like to modify some constant data through the Stateflow API. 
Thus, I want to know if it is possible to add a Stateflow constant data via the API. 

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 29 de Jul. de 2024
Editada: MathWorks Support Team el 29 de Ag. de 2024
Yes, it is possible. 
Please go through the code below to have a better understanding of the commands for the same 
% This script demonstrates simple example of using the stateflow API.
% For more details about the Stateflow commands please go through the
% documentation online
clear;
bdclose('all'); % Close all models
clc;
%% First create a new model
sfnew; % Creates a new model with a new stateflow chart in it
%% Access the model object created
rt = sfroot;
m = rt.find('-isa','Simulink.BlockDiagram');
%% Access the stateflow object in the model
ch = m.find('-isa','Stateflow.Chart');
% Create new states A and B in stateflow
state_A = Stateflow.State(ch);
state_A.Name = 'A';
state_A.Position = [80 120 90 60];
state_B = Stateflow.State(ch);
state_B.Name = 'B';
state_B.Position = [240 120 90 60];
% Create a transition
trans_A2B = Stateflow.Transition(ch);
trans_A2B.Source = state_A;
trans_A2B.Destination = state_B;
trans_A2B.SourceOClock = 3;
trans_A2B.DestinationOClock = 9;
% Add a default transition to state A
default2A = Stateflow.Transition(ch);
default2A.Destination = state_A;
default2A.DestinationOClock = 0;
%% Add input/output and parameters and constants to the chart
% Add an input in1
data_in = Stateflow.Data(ch);
data_in.Scope = 'Input';
data_in.Name = 'in1';
% Add an output out1
data_out = Stateflow.Data(ch);
data_out.Scope = 'Output';
data_out.Name = 'out1';
% Add a constant Param1
data_param = Stateflow.Data(ch);
data_param.Scope = 'Parameter';
data_param.Name = 'Param1';
% Add a constant Const1
data_const = Stateflow.Data(ch);
data_const.Scope = 'Constant';
data_const.Name = 'Const1';
data_const.Props.InitialValue = '17'; % => Change the constant initial value here
% Open the stateflow chart for view
ch.view;
%% Save the model with any name you want
sfsave;
Also, some type of variables like the constant type can only be set before the simulation starts. If you are trying to dynamically change the read-only variables during the simulation for the SIL then, you might get an error.  Please see relevant documentation linked below for read-only variable types:
Manage Data:
Please run the below command in the command window of installed MATLAB R2019a version to get the release specific documentation
>> web(fullfile(docroot, 'stateflow/ug/use-the-symbols-window-with-stateflow-data-events-and-messages.html'))
Set Data Properties:
To find the relevant release specific documentation, run the below command in the command window of installed MATLAB R2019a version:
>> web(fullfile(docroot, 'stateflow/ug/set-data-properties-1.html'))

Más respuestas (0)

Categorías

Más información sobre Stateflow Programmatic Interface en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by