建立系统如下:test.slx
1 获取参数
%% 打开系统,获取系统中模块参数
clc,clear,close all
open_system('test.slx');%打开当前路径下的仿真系统
get_param('test/Sine Wave','Amplitude')%获取振幅
get_param('test/Sine Wave','Sampletime')%获取采样时间
ysw1 = sprintf('\n');%对于名字为两行的情况,先将回车付给字符串ysw1
get_param(['test/Signal',ysw1,'Generator'],'Amplitude') %获取Signal Generator的幅值
close_system('test.slx');%关闭系统
运行结果:
ans =
'2'
ans =
'0.1'
ans =
'1'
2 find_system
%find_system的使用
clc,clear,close all
open_system('test.slx');%打开当前路径下的仿真系统
find_block_diagram = find_system('Type','block_diagram')%返回所有打开的系统名称,此处返回'test'
find_now_diagram=find_system("test",'FirstResultOnly','off')%返回当前打开的系统
close_system('test.slx');%关闭系统
运行结果:
find_block_diagram =
5×1 cell 数组
{'test' }
{'simulink3'}
{'ysw4_1' }
{'hdlsllib' }
{'ysw3_45' }
find_now_diagram =
6×1 cell 数组
{'test' }
{'test/Integrator' }
{'test/Integrator1' }
{'test/Scope' }
{'test/Signal↵Generator'}
{'test/Sine Wave' }
3 save_system
保存模型:
%%
%save_system的使用
clc,clear,close all
open_system('test.slx');%打开系统
save_system('test','test_save')%另存test.slx为test_save.slx
close_system('test.slx');%关闭系统
close_system('test_save.slx');%关闭系统
运行结果:
4 bdclose
无条件关闭所有已打开的模型
bdclose(all)%无条件关闭所有的模型窗口
5 add_block,delete_block
%%
%add_block,delete_block的使用
clc,clear,close all
open_system('test.slx');%打开系统
add_block('Simulink/Sources/Sine Wave','test/Wine Wave4')%test中加入Wine Wave4模块
delete_block('test/Wine Wave4')%删除test中的Wine Wave4模块
close_system('test_save2.slx');%关闭系统
6 建立simulink模型
%%
%add line,delete_line
clc,clear,close all
new_system('test2')
open_system('test2');%打开系统
add_block('Simulink/Sources/Sine Wave','test2/Sine Wave3')%test中加入Wine Wave4模块
add_block('Simulink/Sinks/Scope','test2/Scope3')%test中加入Wine Wave4模块
add_line('test2','Sine Wave3/1','Scope3/1')%连线
delete_line('test2','Sine Wave3/1','Scope3/1')%删除连线
add_line('test2','Sine Wave3/1','Scope3/1')%连线
rep=replace_block('test2','Scope','Simulink/Continuous/Integrator')%将Scope3替换成Integrator3
add_block('Simulink/Sinks/Scope','test2/Scope4')%test中加入Scope4模块
add_line('test2','Scope3/1','Scope4/1')%连线
sim('test2')%运行仿真
test2_path=gcb%返回最近执行到或点击过的模块,返回 'test2/Scope4'
test2_path1=gcb('test2')%返回返回最近执行到或点击过的模块,返回'test2/Scope4'
test2_gcs=gcs%获取当前系统的路径名,返回'test2'
test2_bdroot=('test2/Scope4')%反馈该模块的最高级系统名称,返回 'test2/Scope4'
test2_getparam=get_param('test2/Sine Wave3','Amplitude')%获取Sine Wave3振幅参数,返回1
set_param('test2/Sine Wave3','Amplitude','2')%获取Sine Wave3振幅参数为2
sim('test2',[0 10])%运行仿真,0到第10s
save_system('test2')%保存模型
close_system('test2')%关闭模型
结果:
返回结果:
rep =
1×1 cell 数组
{'test2/Scope3'}
test2_path =
'test2/Scope4'
test2_path1 =
'test2/Scope4'
test2_gcs =
'test2'
test2_bdroot =
'test2/Scope4'
test2_getparam =
'1'