Simulink校长的某一期用的代码
立即下载
资源介绍:
根据校长学习视频敲的,因为B站视频下面评论放不了这么多字符,传这里了。一起向校长学习
分为3个M文件:
Link_and_Delete.m
delete_pair_blocks.m
delete_pair_bl.m
持续学习,未来有你。
视频网址https://www.bilibili.com/video/BV1HA411h7Zn?p=20&vd_source=bfeef05ac3fceabb6b8020f00d6d7c54
%opena new mdl
mdl_name ='link_delete';
mdl_fullname = [mdl_name,'.slx'];
% delete the model generated last time
if exist(mdl_fullname,'file')
try
delete(mdl_fullname);
catch
close_system(mdl_fullname);
delete(mdl_fullname);
end
end
new_system(mdl_name);
save_system(mdl_name);
open_system(mdl_name);
% define some constant
block_lib_path ='simulink/Commonly Used Blocks/';
lib_block_name = {'Gain', 'Integrator','Data Type Conversion','Saturation','Delay','Discrete-Time Integrator'};
% block to be selected
block_type_num=length(lib_block_name);
blocks_num = 5; % generates 5 pairs of blocks
en_num = 500; % parameter for enlarge block position offset
for ii = 1:blocks_num
% generate block index randomly
ind =randi(block_type_num, [1 1]);
% copy block into model file as cell format.
src_str=strcat(block_lib_path, lib_block_name(ind));
dst_str=strcat(mdl_name,'/',lib_block_name(ind));
block_handle = add_block(src_str{:}, dst_str{:}, 'MakeNameUnique', 'on');
% get the handle of new generated block
pos = get_param(block_handle,'position');
% make sure blocks are paired.
add_block(src_str{:}, dst_str{:},'MakeNameUnique', 'on', 'position', pos + en_num * rand);
end
% set port callback for all blocks generated in the model
blocks_handle = find_system(gcs,'findall','on','type','block');
for ii=1:blocks_num*2
% get current block's properties
prop = get(blocks_handle(ii));
% set callback to each port(Inport only)
set(prop.PortHandles.Inport,'ConnectionCallback', 'delete_pair_blocks');
% sset(prop.PortHandles.Outport,'Connectioncallback', 'link_callback4outport');
end