我是靠谱客的博主 个性铃铛,这篇文章主要介绍matlab的syntax简记1.get2.get_param3.add_block4.find_system5.add_line6.canDatabase7.delete_blockZ. Example Program,现在分享给大家,希望可以做个参考。

文章目录

  • 1.get
    • 1.1 Syntax
    • 1.2 Example
    • 1.3 Link
  • 2.get_param
    • 2.1 Syntax
    • 2.2 Example
    • 2.2 Link
  • 3.add_block
    • 3.1 Syntax
    • 3.2 Example
    • 3.3 Link
  • 4.find_system
    • 4.1 Syntax
    • 4.2 Example
    • 4.3 Link
  • 5.add_line
    • 5.1 Syntax
    • 5.2 Example
    • 5.3 Link
  • 6.canDatabase
    • 6.1 Syntax
    • 6.2 Example
    • 6.3 Link
  • 7.delete_block
    • 7.1 Syntax
    • 7.2 Example
    • 7.3 Link
  • Z. Example Program
    • Z.1 获取模块的所有属性
    • Z.2 获得所有端口的位置
    • Z.3 依据源模块名称更改目的模块名称
    • Z.4 删除当前模型下所有连接线
    • Z.5 删除当前模型下所有断线
    • Z.6 获取当前模型下所有连接线及其源、目的端口句柄
    • Z.7 查到某一类型的所有模块并删除
    • Z.8 选中某一类型的所有模块并在其输出端口添加 Scope 模块
    • Z.9 Subsystem
      • Z.9.1 为所有 subsystem 的输出端口添加 Scope
      • Z.9.2 为所有 subsystem 的输入端口添加 Constant
      • Z.9.3 为所有 subsystem 的输出端口添加 Terminator
    • Z.10 删去src_blk_name尾部特定字符串且以删除后的字符串+'_C'为constant名称
    • Z.11 将所有连接线以其连接的 Source Block 名称进行命名
    • Z.12 为所有DataTypeConversion添加 Ground 模块
    • Z.13 为所有 inport 添加 Terminator 模块
    • Z.14 查找当前模型及其子模型中的所有连接线名称及其路径
    • Z.15 输出 Bus Creator 包含的所有信号名称
    • Z.16 对连接 Bus Creator 的信号线以SourceBlockName进行命名

1.get

get: Query graphics object properties.

1.1 Syntax

v = get(h)
v = get(h,propertyName)
v = get(h,propertyArray)
v = get(h,‘default’)
v = get(h,defaultTypeProperty)
v = get(groot,‘factory’)
v = get(groot,factoryTypeProperty)

1.2 Example

  1. get(handle); % 获得 handle 的所有属性。
  2. get(handle.OutPort(x),‘Position’) % 获得输出端口x的位置

1.3 Link

Link-get:get

2.get_param

Get parameter names and values.

2.1 Syntax

ParamValue = get_param(Object,Parameter)

2.2 Example

  1. get_param(‘xxx’,‘Handle’); % 获得模块的句柄,有时候返回的是 cell 格式,需要 cell2mat(h) 才可以使用句柄
  2. get_param(‘xxx’,‘PortHandles’ ) % 获得模块所有端口的句柄
  3. get_param(handle.OutPort(x),‘Position’) % 获得输出端口x的位置
  4. get_param(gcb,‘BlockType’) % 获得当前选中模块的类型

2.2 Link

Link:get_param

3.add_block

Add block to model

3.1 Syntax

h = add_block(source,dest)
h = add_block(source,dest,‘MakeNameUnique’,‘on’)
h = add_block(,‘CopyOption’,‘nolink’)
h = add_block(sourceIn,destIn,‘CopyOption’,‘duplicate’)
h = (
,Name,Value)

3.2 Example

  1. add_block(‘simulink/Sources/In1’,[gcs ‘/In1’]) % 在模型中添加一个输入端口

3.3 Link

add_block

4.find_system

Find systems, blocks, lines, ports, and annotations

4.1 Syntax

Objects = find_system
Objects = find_system(System)
Objects = find_system(Name,Value)
Objects = find_system(System,Name,Value)

4.2 Example

  1. gotoHandles = find_system(gcs, ‘Searchdepth’, 1, ‘FindAll’, ‘on’, ‘BlockType’, ‘Goto’); % 获得当前 gcs 的所有 goto 的句柄

4.3 Link

find_system

5.add_line

Add line to Simulink model.

5.1 Syntax

h = add_line(sys,out,in)
h = add_line(sys,out,in,‘autorouting’,autoOption)
h = add_line(sys,points)

5.2 Example

5.3 Link

add_line

6.canDatabase

Create handle to CAN database file

6.1 Syntax

candb = canDatabase(‘dbfile.dbc’)

6.2 Example

candb = canDatabase(‘E:examplesvntJ1939.dbc’])

6.3 Link

canDatabase

7.delete_block

Delete blocks from Simulink system.

7.1 Syntax

delete_block(blockArg)

7.2 Example

7.3 Link

delete_block

Z. Example Program

Z.1 获取模块的所有属性

复制代码
1
2
3
4
5
% 获得当前鼠标选中模块的句柄 blkNameHandle = get_param(gcb,'Handle'); % 获得句柄的所有属性 get(blkNameHandle)

Z.2 获得所有端口的位置

复制代码
1
2
3
4
5
h = get_param('xxx', 'Handle'); % 获得 block 的句柄 get(h) % 获得 block 的所有属性 portHandles = get(h,''PortHandles'') % 获得所有端口的句柄 get(portHandles.Outport(x),'Position') % 获得输出端口 x 的位置

Z.3 依据源模块名称更改目的模块名称

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
% **************************************************************************** % Copyright : % File name : Change_DstBlock_name_by_SrcName.m % Description: 查找当前模型下所有连接线,并将连接线的 DstBLock 名称依据 SrcBlock 名称更改。 % Author : % Version : V1.0 % Date : 2022.4.2 % History : 0 % ***************************************************************************** % 获得当前系统下,所有连接线的句柄 line_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'Type', 'Line'); line_count = length(line_handles); for i=1:1:line_count % 获得当前系统下,所有连接线的 Source block handle source_block_handles = get_param(line_handles(i), 'SrcBlockHandle'); % 获得当前系统下,所有连接线的 Source block handle 的名称 source_block_name = get_param(source_block_handles, 'Name'); % 获得当前系统下,所有连接线的 Destination block handle destination_block_handles = get_param(line_handles(i), 'DstBlockHandle'); % 将当前系统下,所有DstBLock 名称依据 SrcBlock 名称更改 set_param(destination_block_handles,'Name', ['Scope_' source_block_name]); end

Z.4 删除当前模型下所有连接线

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
% **************************************************************************** % Copyright : % File name : Find_all_line_and_delete.m % Description: 查找当前模型下所有连接线,并进行删除。 % Author : % Version : V1.0 % Date : 2022.4.2 % History : 0 % ***************************************************************************** handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'Type', 'Line') len = length(handles) for i=1:1:len delete_line(handles(i)) end

Z.5 删除当前模型下所有断线

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
% **************************************************************************** % Copyright : % File name : Find_all_line_unconnected_and_delet.m % Description: 查找当前模型下所有未连接的线,并进行删除。 % Author : % Version : V1.0 % Date : 2022.4.14 % History : 0 % ***************************************************************************** % 找到所有已经连接的线的句柄 handles_line_connected = find_system(gcs,'FindAll','on','SearchDepth',1,'Type','line','Connected','on'); count_line_connected = length(handles_line_connected); % 找到所有未连接的线的句柄 handles_line_unconnected = find_system(gcs,'FindAll','on','SearchDepth',1,'Type','line','Connected','off'); count_line_unconnected = length(handles_line_unconnected); % 删除所有未连接的线 for i=1:1:count_line_unconnected delete_line(handles_line_unconnected(i)) end

Z.6 获取当前模型下所有连接线及其源、目的端口句柄

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
% **************************************************************************** % Copyright : % File name : Find_line_or_All_line_handle_and_Src_Dst_Handle.m % Description: 查找当前模型下所有连接线的句柄,及其连接的 Source Block, Destination Block 句柄 % Author : % Version : V1.0 % Date : 2022.4.2 % History : 0 % ***************************************************************************** % 获得当前选中连接线句柄 h_line = find_system(gcs,'FindAll','on','type','line','Selected', 'on'); % 获得连接线的 SrcBlockHandle source_block_handle = get_param(h_line,'SrcBlockHandle'); % 获得连接线的 DstBlockHandle destination_block_handle = get_param(h_line,'DstBlockHandle'); % 获得当前系统下,所有连接线的句柄 line_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'Type', 'Line'); line_count = length(line_handles); % 获得当前系统下,所有连接线的 Source block handle, Destination Block handle for i=1:1:line_count source_block_handles = get_param(line_handles(i),'SrcBlockHandle'); destination_block_handles = get_param(line_handles(i),'DstBlockHandle'); end

Z.7 查到某一类型的所有模块并删除

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
% **************************************************************************** % Copyright : % File name : Find_one_type_all_and_delete.m % Description: 查找当前模型下某一类型的所有模块,并进行删除。 % Author : % Version : V1.0 % Date : 2022.4.2 % History : 0 % ***************************************************************************** % xxx 为模块类型如 Outport, Inport handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'Outport') len = length(handles) for i=1:1:len delete_block(handles(i)) end

Z.8 选中某一类型的所有模块并在其输出端口添加 Scope 模块

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
**************************************************************************** % Copyright : % File name : add_scope_and_align.m % Description: 选中已存在的某一模块,该模块具有输出端口,添加 scope 模块,并完成连线和对齐。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.4.6 % History : 0 % ***************************************************************************** %--------------- src 信息 --------------% % 连接线长度 line_length = 50; % 选中模块句柄 src_block_handle = get_param(gcb,'Handle'); % 选中该模块端口句柄 src_port_handles = get_param(src_block_handle,'PortHandles'); % 取 position 属性 src_outport_position = get(src_port_handles.Outport,'Position'); %--------------- dst 信息 --------------% % 添加 scope 模块,命名为 'scope' add_block('simulink/Sinks/Scope', [gcs '/scope'], 'MakeNameUnique', 'on') % 获得添加模块的句柄 dst_block_handle = get_param([gcs '/scope'],'Handle'); % 获得添加模块的端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 position 属性 dst_inport_position = get(dst_port_handles.Inport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; %--------------- 对 src, dst 操作 --------------% % 端口连线 add_line(gcs, src_port_handles.Outport, dst_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x + line_length, src_outport_y - dst_height / 2, src_outport_x + line_length + dst_width, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_outport_position(1)+line_length, src_outport_position(2)-dst_height/2 src_outport_position(1)+line_length+dst_width, src_outport_position(2)+dst_height/2])

Z.9 Subsystem

同一层级存在其他 subsystem,将其注释,然后直接使用脚本。

Z.9.1 为所有 subsystem 的输出端口添加 Scope

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
% **************************************************************************** % Copyright : % File name : Many_SubSystems_block_exit_and_add_Scope.m % Description: 模型中存在较多只有一个输出端口的 block(注意 block 类型为 Subsystem), % 一一对应添加 Scope 模块,且命名为 Scope_子系统名称,并完成连线和对齐。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.4.6 % History : 0 % ***************************************************************************** % 连接线长度 line_length = 50; % 根据要操作的内容是否存在于子系统中进行选择,若存在于子系统中则设置为 2,否则为1 loop_start_index = 1; %--------------- src 信息 --------------% src_block_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'SubSystem'); src_block_count = length(src_block_handles); % 如果 gcs 本身是 SubSystem,则 length 数量会多一个,且 Index == 1 %--------------- dst 信息 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 src_block_name = get(src_block_handles(i),'Name'); % 添加 scope 模块,命名为 'scope_xxx' add_block('simulink/Sinks/Scope', [gcs '/Scope_' src_block_name], 'MakeNameUnique', 'on') end %--------------- 对 src, dst 操作 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 %------------- src 操作 ----------------% % 取 src 端口句柄 src_port_handles = get(src_block_handles(i), 'PortHandles'); % 取 src 输出端口的 position 属性 src_outport_position = get(src_port_handles.Outport,'Position'); src_block_name = get(src_block_handles(i),'Name'); %------------- dst 操作 ----------------% % 取 dst 句柄 dst_block_handle = get_param([gcs '/Scope_' src_block_name],'Handle'); % 获得 dst 端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 dst 输入端口的 position 属性 dst_inport_position = get(dst_port_handles.Inport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; % 端口连线 add_line(gcs, src_port_handles.Outport, dst_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x + line_length, src_outport_y - dst_height / 2, src_outport_x + line_length + dst_width, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_outport_position(1)+line_length, src_outport_position(2)-dst_height/2 src_outport_position(1)+line_length+dst_width, src_outport_position(2)+dst_height/2]) end

Z.9.2 为所有 subsystem 的输入端口添加 Constant

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
% **************************************************************************** % Copyright : % File name : Many_SubSystems_block_exit_and_add_Constant.m % Description: 模型中存在较多只有一个输入端口的 block(注意 block 类型为 Subsystem), % 一一对应添加 Constant 模块,且命名为 Constant_子系统名称,初始值为零,并完成连线和对齐。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.4.6 % History : 0 % ***************************************************************************** % 连接线长度 line_length = 50; % 根据要操作的内容是否存在于子系统中进行选择,若存在于子系统中则设置为 2,否则为1 loop_start_index = 1; %--------------- src 信息 --------------% src_block_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'SubSystem'); src_block_count = length(src_block_handles); % 如果 gcs 本身是 SubSystem,则 length 数量会多一个,且 Index == 1 %--------------- dst 信息 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 src_block_name = get(src_block_handles(i),'Name'); % 添加 scope 模块,命名为 'scope_xxx' add_block('simulink/Sources/Constant', [gcs '/Constant_' src_block_name], 'MakeNameUnique', 'on') end %--------------- 对 src, dst 操作 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 %------------- src 操作 ----------------% % 取 src 端口句柄 src_port_handles = get(src_block_handles(i), 'PortHandles'); % 取 src 输入端口的 position 属性 src_inport_position = get(src_port_handles.Inport,'Position'); src_block_name = get(src_block_handles(i),'Name'); %------------- dst 操作 ----------------% % 取 dst 句柄 dst_block_handle = get_param([gcs '/Constant_' src_block_name],'Handle'); % 设置初始值为零 set(dst_block_handle,'Value','0') % 获得 dst 端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 dst 输出端口的 position 属性 dst_outport_position = get(dst_port_handles.Outport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; % 端口连线 add_line(gcs, dst_port_handles.Outport, src_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x - line_length - dst_width, src_outport_y - dst_height / 2, src_outport_x - line_length, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_inport_position(1)-line_length-dst_width, src_inport_position(2)-dst_height/2, src_inport_position(1)-line_length, src_inport_position(2)+dst_height/2]) end

Z.9.3 为所有 subsystem 的输出端口添加 Terminator

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
% **************************************************************************** % Copyright : % File name : Many_SubSystems_block_exit_and_add_Terminator.m % Description: 模型中存在较多只有一个输出端口的 block(注意 block 类型为 Subsystem), % 一一对应添加 Terminator 模块,且命名为 Terminator_ 子系统名称,并完成连线和对齐。 % 注意: 如果同一层中存在其他 SubSystem,但又不需要添加`Terminator`,将其屏蔽即可。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.6.20 % History : 0 % ***************************************************************************** % 连接线长度 line_length = 50; % 根据要操作的内容是否存在于子系统中进行选择,若存在于子系统中则设置为 2,否则为1 loop_start_index = 1; %--------------- src 信息 --------------% src_block_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'SubSystem'); src_block_count = length(src_block_handles); % 如果 gcs 本身是 SubSystem,则 length 数量会多一个,且 Index == 1 %--------------- dst 信息 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 src_block_name = get(src_block_handles(i),'Name'); % 添加 scope 模块,命名为 'scope_xxx' add_block('simulink/Sinks/Terminator', [gcs '/Terminator_' src_block_name], 'MakeNameUnique', 'on') end %--------------- 对 src, dst 操作 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 %------------- src 操作 ----------------% % 取 src 端口句柄 src_port_handles = get(src_block_handles(i), 'PortHandles'); % 取 src 输出端口的 position 属性 src_outport_position = get(src_port_handles.Outport,'Position'); src_block_name = get(src_block_handles(i),'Name'); %------------- dst 操作 ----------------% % 取 dst 句柄 dst_block_handle = get_param([gcs '/Terminator_' src_block_name],'Handle'); % 获得 dst 端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 dst 输入端口的 position 属性 dst_inport_position = get(dst_port_handles.Inport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; % 端口连线 add_line(gcs, src_port_handles.Outport, dst_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x + line_length, src_outport_y - dst_height / 2, src_outport_x + line_length + dst_width, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_outport_position(1)+line_length, src_outport_position(2)-dst_height/2 src_outport_position(1)+line_length+dst_width, src_outport_position(2)+dst_height/2]) end

Z.10 删去src_blk_name尾部特定字符串且以删除后的字符串+'_C’为constant名称

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
% **************************************************************************** % Copyright : % File name : Many_SubSystems_block_exit_and_add_Constant.m % Description: 模型中存在较多只有一个输入端口的 block(注意 block 类型为 Subsystem), % 一一对应添加 Constant 模块,且 % 1. 删除子系统名称尾部特定字符串 % 2. Constant 模块命名为 <删除特定字符串的子系统>+'_C',初始值为零,并完成连线和对齐。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.5.22 % History : 0 % ***************************************************************************** % 连接线长度 line_length = 50; % 根据要操作的内容是否存在于子系统中进行选择,若存在于子系统中则设置为 2,否则为1 loop_start_index = 1; %--------------- src 信息 --------------% src_block_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'SubSystem'); src_block_count = length(src_block_handles); % 如果 gcs 本身是 SubSystem,则 length 数量会多一个,且 Index == 1 %--------------- dst 信息 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 src_block_name = get(src_block_handles(i),'Name'); % 添加 scope 模块,命名为 'scope_xxx' add_block('simulink/Sources/Constant', [gcs '/' erase(src_block_name,'_PHY') '_C'], 'MakeNameUnique', 'on') end %--------------- 对 src, dst 操作 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 %------------- src 操作 ----------------% % 取 src 端口句柄 src_port_handles = get(src_block_handles(i), 'PortHandles'); % 取 src 输入端口的 position 属性 src_inport_position = get(src_port_handles.Inport,'Position'); src_block_name = get(src_block_handles(i),'Name'); %------------- dst 操作 ----------------% % 取 dst 句柄 dst_block_handle = get_param([gcs '/' erase(src_block_name,'_PHY') '_C'],'Handle'); % 设置初始值为零 set(dst_block_handle,'Value','0') % 获得 dst 端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 dst 输出端口的 position 属性 dst_outport_position = get(dst_port_handles.Outport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; % 端口连线 add_line(gcs, dst_port_handles.Outport, src_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x - line_length - dst_width, src_outport_y - dst_height / 2, src_outport_x - line_length, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_inport_position(1)-line_length-dst_width, src_inport_position(2)-dst_height/2, src_inport_position(1)-line_length, src_inport_position(2)+dst_height/2]) end

Z.11 将所有连接线以其连接的 Source Block 名称进行命名

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
% **************************************************************************** % Copyright : % File name : Find_line_And_Named.m % Description: 查找当前模型下所有连接线,并以其连接的 Source Block 名称对其命名。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.4.8 % History : 0 % ***************************************************************************** % 获得当前选中连接线句柄 h_line = find_system(gcs,'FindAll','on','type','line','Selected', 'on'); % 获得连接线的 SrcBlockHandle source_block_handle = get_param(h_line,'SrcBlockHandle'); % 获得连接线的 DstBlockHandle destination_block_handle = get_param(h_line,'DstBlockHandle'); % 以 source block 名称对信号线命名 src_name = get(source_block_handle,'name') set(h_line,'name',src_name) % 获得当前系统下,所有连接线的句柄 line_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'Type', 'Line'); line_count = length(line_handles); % 获得当前系统下,所有连接线的 Source block handle, Destination Block handle for i=1:1:line_count source_block_handles = get_param(line_handles(i),'SrcBlockHandle'); src_block_name = get(source_block_handles,'name') set(line_handles(i),'name',src_block_name) end

Z.12 为所有DataTypeConversion添加 Ground 模块

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
% **************************************************************************** % Copyright : % File name : Many_DataTypeConversion_block_exit_and_add_Ground.m % Description: 模型中存在较多只有一个输入端口的 block(注意 block 类型为 DataTypeConversion), % 一一对应添加 Ground 模块,且命名为 Ground_子系统名称,并完成连线和对齐。 % Author : % Version : V1.0 % Date : 2022.4.14 % History : 0 % ***************************************************************************** % 连接线长度 line_length = 50; % 根据要操作的内容是否存在于子系统中进行选择,若存在于子系统中则设置为 2,否则为1 loop_start_index = 1; %--------------- src 信息 --------------% src_block_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'DataTypeConversion'); src_block_count = length(src_block_handles); % 如果 gcs 本身是 SubSystem,则 length 数量会多一个,且 Index == 1 %--------------- dst 信息 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 src_block_name = get(src_block_handles(i),'Name'); % 添加 Ground 模块,命名为 'Ground_xxx' add_block('simulink/Sources/Ground', [gcs '/Ground' src_block_name], 'MakeNameUnique', 'on') end %--------------- 对 src, dst 操作 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 %------------- src 操作 ----------------% % 取 src 端口句柄 src_port_handles = get(src_block_handles(i), 'PortHandles'); % 取 src 输入端口的 position 属性 src_inport_position = get(src_port_handles.Inport,'Position'); src_block_name = get(src_block_handles(i),'Name'); %------------- dst 操作 ----------------% % 取 dst 句柄 dst_block_handle = get_param([gcs '/Ground' src_block_name],'Handle'); % 获得 dst 端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 dst 输出端口的 position 属性 dst_outport_position = get(dst_port_handles.Outport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; % 端口连线 add_line(gcs, dst_port_handles.Outport, src_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x - line_length - dst_width, src_outport_y - dst_height / 2, src_outport_x - line_length, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_inport_position(1)-line_length-dst_width, src_inport_position(2)-dst_height/2, src_inport_position(1)-line_length, src_inport_position(2)+dst_height/2]) end

Z.13 为所有 inport 添加 Terminator 模块

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
% **************************************************************************** % Copyright : % File name : Many_Inport_block_exit_and_add_Terminator.m % Description: 模型中存在较多或只有一个 Inport,一一对应添加 Terminator 模块, % 且命名为 Terminator_子系统名称,并完成连线和对齐。 % Author : % Version : V1.0 % Date : 2022.4.14 % History : 0 % ***************************************************************************** % 连接线长度 line_length = 50; % 根据要操作的内容是否存在于子系统中进行选择,若存在于子系统中则设置为 2,否则为1 loop_start_index = 1; %--------------- src 信息 --------------% src_block_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'BlockType', 'Inport'); src_block_count = length(src_block_handles); % 如果 gcs 本身是 SubSystem,则 length 数量会多一个,且 Index == 1 %--------------- dst 信息 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 src_block_name = get(src_block_handles(i),'Name'); % 添加 Terminator 模块,命名为 'Terminator_xxx' add_block('simulink/Sinks/Terminator', [gcs '/Terminator_' src_block_name], 'MakeNameUnique', 'on') end %--------------- 对 src, dst 操作 --------------% for i=loop_start_index:1:src_block_count % 根据情况是否选择不要第一个 %------------- src 操作 ----------------% % 取 src 端口句柄 src_port_handles = get(src_block_handles(i), 'PortHandles'); % 取 src 输出端口的 position 属性 src_outport_position = get(src_port_handles.Outport,'Position'); src_block_name = get(src_block_handles(i),'Name'); %------------- dst 操作 ----------------% % 取 dst 句柄 dst_block_handle = get_param([gcs '/Terminator_' src_block_name],'Handle'); % 获得 dst 端口句柄 dst_port_handles = get_param(dst_block_handle,'PortHandles'); % 取 dst 输入端口的 position 属性 dst_inport_position = get(dst_port_handles.Inport,'Position'); % [left top right bottom] 获得模块的位置 dst_position = get(dst_block_handle,'Position'); dst_pos_left = dst_position(1); dst_pos_top = dst_position(2); dst_pos_right = dst_position(3); dst_pos_bottom = dst_position(4); % 获得模块的宽和高 dst_width = dst_pos_right - dst_pos_left; dst_height = dst_pos_bottom - dst_pos_top; % 端口连线 add_line(gcs, src_port_handles.Outport, dst_port_handles.Inport) % 对齐模块, (src_outport_x,src_outport_y) --> [src_outport_x + line_length, src_outport_y - dst_height / 2, src_outport_x + line_length + dst_width, src_outport_y + dst_height / 2] set(dst_block_handle,'Position', [src_outport_position(1)+line_length, src_outport_position(2)-dst_height/2 src_outport_position(1)+line_length+dst_width, src_outport_position(2)+dst_height/2]) end

Z.14 查找当前模型及其子模型中的所有连接线名称及其路径

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
% **************************************************************************** % Copyright : % File name : Find_all_line_and_path.m % Description: 查找当前模型及其子模型中的所有连接线名称及其路径。 % Author : % Version : V1.0 % Date : 2022.4.19 % History : 0 % ***************************************************************************** handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 2, 'Type', 'Line') result = '' len = length(handles) for i=1:1:len result_1 = get_param(handles(i),'name'); result_2 = get_param(handles(i),'Parent'); result = [result result_1 ',' result_2 ';']; end

Z.15 输出 Bus Creator 包含的所有信号名称

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
% **************************************************************************** % Copyright : % File name : Find_all_line_and_path.m % Description: 选中 Bus Creator 并输出其包含的所有信号名称(注意要选中 Bus Creator) % Author : % Version : V1.0 % Date : 2022.5.10 % History : 0 % ***************************************************************************** handle = get_param(gcb,'handle'); signal_names = get(handle,'InputSignalNames'); count = length(signal_names); name_list = ''; for i=1:1:length(signal_names) temp_name = [signal_names{i}]; name_list = [name_list, temp_name, ', ']; end

Z.16 对连接 Bus Creator 的信号线以SourceBlockName进行命名

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
% **************************************************************************** % Copyright : % File name : Find_Connected_to_BusCreator_Line_And_Named.m % Description: 选中 Bus Creator,对其连接的信号线以 Source Block 名称对信号线命名。 % Author : Zhiqiong Cao % Version : V1.0 % Date : 2022.5.20 % History : 0 % ***************************************************************************** % 获得选中的 Bus Creator 名称 selected_block_name = get_param(gcb,'name'); % 获得当前系统下,所有连接线的句柄 line_handles = find_system(gcs, 'FindAll', 'on', 'SearchDepth', 1, 'Type', 'Line'); line_count = length(line_handles); % 获得当前系统下,所有连接线的 Source block handle, Destination Block handle for i=1:1:line_count % 获得连接线的 source block handle source_block_handles = get_param(line_handles(i),'SrcBlockHandle'); % 获得连接线的 destination block handle destination_block_handles = get_param(line_handles(i),'DstBlockHandle'); % 获得连接线的 source block name src_block_name = get(source_block_handles,'name'); % 获得连接线的 destination block name dst_block_name = get(destination_block_handles,'name'); % 在这里做筛选,如果 destination block 是选中的 Bus Creator,则 % 以该信号线的 source block name 作为信号线名 if strcmp(selected_block_name,dst_block_name) set(line_handles(i),'name',src_block_name) end end

最后

以上就是个性铃铛最近收集整理的关于matlab的syntax简记1.get2.get_param3.add_block4.find_system5.add_line6.canDatabase7.delete_blockZ. Example Program的全部内容,更多相关matlab的syntax简记1.get2.get_param3.add_block4.find_system5.add_line6.canDatabase7.delete_blockZ.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部