✅作者简介：热爱科研的算法开发者，Python、Matlab项目可交流、沟通、学习。

🍎个人主页：算法工程师的学习日志

Matlab中可以通过两种方法以编程方式仿真模型：

• 通过 sim 命令。
• 通过运行按钮

`sim` 命令仿真

#### 指定参数名称-值对组

``````simOut = sim('vdp','SimulationMode','normal',...
'SaveState','on','StateSaveName','xout',...
'SaveOutput','on','OutputSaveName','yout',...
'SaveFormat', 'Dataset');
outputs = simOut.yout
outputs =

Characteristics:
Name: 'yout'
Total Elements: 2

Elements:
1 : 'x1'
2 : 'x2'

-Use get or getElement to access elements by index or name.

``````x1 = (outputs.get('x1').Values);
x2 = (outputs.get('x2').Values);
plot(x1); hold on;
plot(x2);
title('VDP States')
xlabel('Time'); legend('x1','x2')``````

#### 启用仿真超时

``````N = 100;
for i = 1:N
simOut(i) = sim('vdp', 'timeout', 1000);
end``````

#### 捕获仿真错误

``````simOut = sim('my_model', 'CaptureErrors', 'on');
ans =

struct with fields:

StopEvent: 'DiagnosticError'
StopEventSource: []
StopEventDescription: 'Division by zero in 'my_model/Divide''
ErrorDiagnostic: [1×1 struct]
WarningDiagnostics: [0×1 struct]``````

#### 访问仿真元数据

``````simOut = sim('vdp','SimulationMode','normal','AbsTol','1e-5',...
'SaveState','on','StateSaveName','xoutNew',...
'SaveOutput','on','OutputSaveName','youtNew',...
'SaveFormat', 'StructureWithTime');``````

``````simOut.getSimulationMetadata.ModelInfo
ans =

struct with fields:

ModelName: 'vdp'
ModelVersion: '1.6'
ModelFilePath: 'C:\MyWork'
UserID: 'User'
MachineName: 'MyMachine'
Platform: 'PCWIN64'
ModelStructuralChecksum: [4×1 uint32]
SimulationMode: 'normal'
StartTime: 0
StopTime: 20
SolverInfo: [1×1 struct]
LoggingInfo: [1×1 struct]``````

``````simOut.getSimulationMetadata.ModelInfo.SolverInfo
ans =

struct with fields:

Type: 'Variable-Step'
Solver: 'ode45'
MaxStepSize: 0.4000``````

``````simOut.getSimulationMetadata.TimingInfo
ans =

struct with fields:

WallClockTimestampStart: '2016-06-17 10:26:58.433686'
WallClockTimestampStop: '2016-06-17 10:26:58.620687'
InitializationElapsedWallTime: 0.1830
ExecutionElapsedWallTime: 1.0000e-03
TerminationElapsedWallTime: 0.0030
TotalElapsedWallTime: 0.1870``````

``````simOut=simOut.setUserString('Results from simulation 1 of 10');
ans =

ModelInfo: [1×1 struct]
TimingInfo: [1×1 struct]
ExecutionInfo: [1×1 struct]
UserString: 'Results from simulation 1 of 10'
UserData: []``````

### “运行”按钮仿真

#### 控制和检查仿真的状态

`set_param` 允许动态更新变量以及将数据记录变量写入工作区。

``set_param('vdp','SimulationCommand','start')``

``````set_param('vdp','SimulationCommand','pause')
set_param('vdp','SimulationCommand','continue')
set_param('vdp','SimulationCommand','stop')``````

``get_param('vdp','SimulationStatus')``

``set_param('vdp','SimulationCommand','update')``

``set_param('vdp','SimulationCommand','WriteDataLogs')``

#### 使用回调自动执行仿真任务

``````% openscopes.m
% Brings scopes to forefront at beginning of simulation.

blocks = find_system(bdroot,'BlockType','Scope');

% Finds all of the scope blocks in the top level of your
% model. To find scopes in subsystems, provide the subsystem
% names. Type help find_system for more on this command.

for i = 1:length(blocks)
set_param(blocks{i},'Open','on')
end

% Loops through all of the scope blocks and brings them
% to the forefront.``````

``set_param('my_model','StartFcn','openscopes')``