-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrename_port_to_sig.m
89 lines (78 loc) · 3.58 KB
/
rename_port_to_sig.m
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
%------------------------------------------------------------------------------
% Simulink scrip for rename the blocks' name to signals' name
% MATLAB version: R2017a
% Author : Shibo Jiang
% Time : 2017/12/20
% Version : Initial -0.1
% Support goto & from block
% Code refactoring -0.2
% Instructions :
%------------------------------------------------------------------------------
function rename_ports_result = rename_port_to_sig()
paraModel = bdroot;
% Original matalb version is R2017a
% 检查Matlab版本是否为R202017a
CorrectVersion_win = '9.2.0.556344 (R2017a)'; % windows
CorrectVersion_linux = '9.2.0.538062 (R2017a)'; % linux
CurrentVersion = version;
if 1 ~= bitor(strcmp(CorrectVersion_win, CurrentVersion),...
strcmp(CorrectVersion_linux, CurrentVersion))
warning('Matlab version mismatch, this scrip should be used for Matlab R2017a');
end
% Original environment character encoding: GBK
% 脚本编码环境是否为GBK
% if ~strcmpi(get_param(0, 'CharacterEncoding'), 'GBK')
% warning('Simulink:EncodingUnMatched', 'The target character...
% encoding (%s) is different from the original (%s).',...
% get_param(0, 'CharacterEncoding'), 'GBK');
% end
% find all inport block
inport_block = find_system(paraModel,'FindAll','on','BlockType','Inport');
% find all outport block
outport_block = find_system(paraModel,'FindAll','on',...
'BlockType','Outport');
% Find all Goto block
goto_block = find_system(paraModel, 'FindAll','on','BlockType','Goto');
% Find all From block
from_block = find_system(paraModel,'FindAll','on','BlockType','From');
% rename inport blocks to signals' name
SetPortName(inport_block, 'OutputSignalNames', 'Name');
% rename outport blocks to signals' name
SetPortName(outport_block, 'InputSignalNames', 'Name');
% Rename Goto blocks to signals's name
SetPortName(goto_block, 'InputSignalNames','GotoTag');
% Rename From blocks to signals's name
SetPortName(from_block, 'OutputSignalNames','GotoTag');
rename_ports_result = 'rename port to signal name successful';
end
%-----------------End of function----------------------------------------------
%-----------Start of function--------------------------------------------------
function SetPortName(block, get_type, set_type)
if isempty(block)
% Do nothing
else
length_block = length(block);
for i = 1:length_block
current_sig_name = get(block(i), get_type);
% translate cell to string
current_sig_name = cell2mat(current_sig_name);
if 1 == isempty(current_sig_name)
% Do nothing. output line has no name defined.
else
% find if tag symbol '<' exit
if strcmp('<',current_sig_name(1))
% remove the tag symbol '< >'
current_sig_name = current_sig_name(2:end-1);
else
% Do nothing
end
% rename
try
set(block(i), set_type, current_sig_name);
catch
end
end
end
end
end
%-----------------End of function----------------------------------------------