-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathchange_parameter.m
367 lines (344 loc) · 15.7 KB
/
change_parameter.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
%------------------------------------------------------------------------------
% Simulink scrip for rename parameters and change parameter's value.
% MATLAB : R2017a
% Author : Shibo Jiang
% Version : 0.7
% Time : 2017/12/20
% Instructions : Add change stateflow parameter function.
% Add creat new parameter function. - 0.3
% Fix bugs ,message can report clearly - 0.4
% Code refactoring - 0.5
% Add state flow parameter changed
% Add storge calss change - 0.6
% Support goto & from block - 0.7
% Support under masked block changing name - 0.8
%------------------------------------------------------------------------------
function output = change_parameter()
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
% % Get inter parameter which defined in model workspace
% simulink_var_space = get_param(paraModel,'ModelWorkspace');
% simulink_par_inter = simulink_var_space.whos;
% Define list file's name
filename = [paraModel,'_list.xlsx'];
% Open outlink parameter which defined in data dictionary
data_dictionary = get_param(paraModel, 'DataDictionary');
current_dic = Simulink.data.dictionary.open(data_dictionary);
dic_entry = getSection(current_dic, 'Design Data');
% Find data in dictionary&model and change name
% Import excel file's data
[number_par, name_par] = xlsread(filename, 'dict');
[number_line, name_line] = xlsread(filename, 'only_line');
NAME_START_ROW = 2;
STR_COLUMN_END = 3;
% Jduge only_line & dict sheet is empty?
if isempty(number_par)
temp_judge_par = 2;
else
temp_judge_par = 0;
end
if isempty(number_line)
temp_judge_line = 1;
else
temp_judge_line = 0;
end
switch (temp_judge_line + temp_judge_par)
case 0
name_all = [name_par(NAME_START_ROW:end,1:STR_COLUMN_END);...
name_line(NAME_START_ROW:end,1:STR_COLUMN_END)];
number_all = number_par(end,1) + number_line(end,1);
case 1
name_all = name_par(NAME_START_ROW:end,1:STR_COLUMN_END);
number_all = number_par(end,1);
case 2
name_all = name_line(NAME_START_ROW:end,1:STR_COLUMN_END);
number_all = number_line(end,1);
case 3
name_all = [];
end
% Whether writing paramter's name
OLD_NAME_COL = 2;
NEW_NAME_COL = 3;
if ~isempty(name_all)
i_diff_name = 1;
for i = 1:number_all(end)
temp_old = name_all{i, OLD_NAME_COL};
temp_new = name_all{i, NEW_NAME_COL};
if ~strcmp(temp_new, temp_old)
% Find the parameter name in dict
par_dict = find(dic_entry, 'Name', temp_old);
% Find the parameter name in signal line
par_signal = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','type','line', 'Name',temp_old);
% % Find the parameter name in constant block
% par_const = find_system(paraModel,'FindAll','on',...
% 'LookUnderMasks','on','FollowLinks','on','BlockType', 'Constant','Value', temp_old);
% Find the paremeter name in constant which include dict constant
par_const = [];
dict_const_block = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType', 'Constant');
if ~isempty(dict_const_block)
par_const = zeros(1,length(dict_const_block));
for temp_index = 1:length(dict_const_block)
temp_const_name = get(dict_const_block(temp_index),'Value');
if strcmp(temp_old, temp_const_name)
par_const(temp_index) = dict_const_block(temp_index);
break;
end
end
end
% Find the parameter name in table block
par_nd_table = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType', 'Lookup_n-D','Table', temp_old);
par_direct_table = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType','LookupNDDirect',...
'Table', temp_old);
% Find the parameter name in port block
par_inport = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType', 'Inport','Name', temp_old);
par_outport = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType', 'Outport','Name', temp_old);
% Find the parameter name in stateflow
sf = sfroot;
par_sf_data = sf.find('-isa','Stateflow.Data',...
'Name', temp_old);
% Find the goto block
par_goto = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType', 'Goto','GotoTag', temp_old);
% Find the from block
par_from = find_system(paraModel,'FindAll','on',...
'LookUnderMasks','on','FollowLinks','on','BlockType', 'From','GotoTag', temp_old);
% Set new name
try
par_dict.Name = temp_new;
end
SetNewName(par_signal, 'Name', temp_new);
SetNewName(par_inport, 'Name', temp_new);
SetNewName(par_outport, 'Name', temp_new);
SetNewName(par_const, 'Value', temp_new);
SetNewName(par_nd_table, 'Table', temp_new);
SetNewName(par_direct_table, 'Table', temp_new);
SetNewName(par_sf_data, 'Name', temp_new);
SetNewName(par_goto, 'GotoTag', temp_new);
SetNewName(par_from, 'GotoTag', temp_new);
% Store diff name for changing value
old_name{i_diff_name} = temp_old;
new_name{i_diff_name} = temp_new;
i_diff_name = i_diff_name + 1;
else
% Do nothing
end
end
output = 'Rename parameter successful.'
else
output = 'No parameter name changed.'
end
% Find simulink parameter in list and change the datatype&value
[num_simu_par, str_simu_par] = xlsread(filename, 'simu_parameter');
SIMU_PAR_NAME_COL = 2;
SIMU_PAR_DATATYPE_COL = 4;
SIMU_PAR_STORGE = 5;
SIMU_PAR_VALUE = 6;
if ~isempty(num_simu_par)
for i = 1:num_simu_par(end,1)
temp_simu_par_name = str_simu_par(i+1, SIMU_PAR_NAME_COL);
temp_simu_par_datatype = str_simu_par(i+1, SIMU_PAR_DATATYPE_COL);
temp_simu_par_storge = str_simu_par(i+1, SIMU_PAR_STORGE);
temp_simu_par_value = num_simu_par(i, SIMU_PAR_VALUE);
% Set new property
temp_return = SetDictParameter(dic_entry,...
temp_simu_par_name{1},...
temp_simu_par_datatype{1},...
temp_simu_par_storge{1},...
temp_simu_par_value);
% Report whether set successful
if strcmp('Invalid name', temp_return)
% Use new name to try again
% Judge whether need find new name
if 1 < i_diff_name
% Find new name
for i = 1:(i_diff_name-1)
if strcmp(old_name{i}, temp_simu_par_name{1})
temp_simu_par_name{1} = new_name{i};
break;
end
end
end
% Creat new entry in dict
initial_value = Simulink.Parameter;
% Try creat new ,if parameter does not exit.
try
addEntry(dic_entry, temp_simu_par_name{1}, initial_value);
end
% Set entry's property
SetDictParameter(dic_entry,...
temp_simu_par_name{1},...
temp_simu_par_datatype{1},...
temp_simu_par_storge{1},...
temp_simu_par_value);
output = ['The parameter "' ,...
temp_simu_par_name{1},...
'" does not exit, have already created it.']
end
end
output = 'Change simulink parameter successful.'
else
output = 'No simulink parameter need changed.'
end
% Find table in list and change the datatype&value
[num_simu_table, str_simu_table] = xlsread(filename, 'simu_table');
TABLE_NAME_COL = 2;
TABLE_DATATYPE_COL = 4;
TABLE_STORGE_COL = 5;
TABLE_ROW_SPACE_COL = 6;
TABLE_COL_SPACE_COL = 7;
TABLE_VALUE_COL = 8;
% Judge whether need writing talbe data
if ~isempty(num_simu_table)
len_simu_table_temp = length(num_simu_table(:,1));
% Calculate cycle time
for i = 1: len_simu_table_temp
if ~strcmp('NaN', num2str(num_simu_table(len_simu_table_temp+1-i,1)))
cycle_time_tale = num_simu_table(len_simu_table_temp+1-i,1);
break;
end
end
% Read tabel data and write to dict
temp_row = 1;
for i = 1: cycle_time_tale
temp_table_name = str_simu_table(1+temp_row, TABLE_NAME_COL);
temp_table_datatype = str_simu_table(1+temp_row, TABLE_DATATYPE_COL);
temp_table_storge = str_simu_table(1+temp_row, TABLE_STORGE_COL);
temp_row_spacing = num_simu_table(temp_row, TABLE_ROW_SPACE_COL);
temp_column_spacing = num_simu_table(temp_row, TABLE_COL_SPACE_COL);
temp_table_data = num_simu_table(temp_row:...
(temp_row + temp_row_spacing - 1),...
TABLE_VALUE_COL:(TABLE_VALUE_COL + temp_column_spacing - 1));
% Set new property
temp_return = SetDictParameter(dic_entry,...
temp_table_name{1},...
temp_table_datatype{1},...
temp_table_storge{1},...
temp_table_data);
% Report whether creat new parameter
if strcmp('Invalid name', temp_return)
% Use new name to try again
% Judge whether need find new name
if 1 < i_diff_name
% Find new name
for i = 1:(i_diff_name-1)
if strcmp(old_name{i}, temp_table_name{1})
temp_table_name{1} = new_name{i};
break;
end
end
end
% Creat new entry in dict
initial_value = Simulink.Parameter;
% Try creat new ,if parameter does not exit.
try
addEntry(dic_entry, temp_table_name{1}, initial_value);
end
% Set entry's property
SetDictParameter(dic_entry,...
temp_table_name{1},...
temp_table_datatype{1},...
temp_table_storge{1},...
temp_table_data);
output = ['The parameter "' ,...
temp_table_name{1},...
'" does not exit, have already created it.']
end
temp_row = temp_row + temp_row_spacing;
end
output = 'Change table data successful.'
else
output = 'No table data changed.'
end
% Find stateflow in the excel,and change it.
[num_sf_par, str_sf_par] = xlsread(filename, 'sf_parameter');
SF_NAME_COL = 2;
SF_SCOPE_COL = 3;
SF_DATATYPE_COL = 4;
if isempty(num_sf_par)
output = 'No state flow parameter need changed.'
else
for i = 1:num_sf_par(end,1)
temp_sf_name = str_sf_par(i+1, SF_NAME_COL);
temp_sf_scope = str_sf_par(i+1, SF_SCOPE_COL);
temp_sf_datatype = str_sf_par(i+1, SF_DATATYPE_COL);
% Find state flow parameter in model
sf = sfroot;
temp_sf_par = sf.find('-isa','Stateflow.Data','Name',temp_sf_name{1});
if isempty(temp_sf_par)
% Report this state flow need defined in matlab
output = ['The parameter "', temp_sf_name{1},...
'" does not exit, it needs be defined in state flow']
else
% Change sf parameter
set(temp_sf_par, 'Scope', temp_sf_scope{1},...
'DataType', temp_sf_datatype{1});
end
end
output = 'Change state flow parameter successful.'
end
% Save changes to the dictionary and close it.
saveChanges(current_dic);
close(current_dic);
% Report
output = output;
end
%-----------------End of function----------------------------------------------
%-----------Start of function--------------------------------------------------
function SetNewName(input, item, new_name)
if isempty(input)
% Do Nothing
else
if 1 < length(input)
for i = 1:length(input)
try
set_param(input(i), item, new_name);
catch
try
set(input(i), item, new_name);
end
end
end
else
try
set_param(input, item, new_name);
catch
try
set(input, item, new_name);
end
end
end
end
end
%-----------------End of function----------------------------------------------
%-----------Start of function--------------------------------------------------
function output = SetDictParameter(entry, name, data_type, storge, value)
% Find the parameter name in dict
simu_par = find(entry, 'Name', name);
if isempty(simu_par)
output = 'Invalid name';
else
output = 'Valid name';
temp_change = getValue(simu_par);
temp_change.DataType = data_type;
temp_change.CoderInfo.StorageClass = storge;
temp_change.Value = value;
% Write dict
setValue(simu_par, temp_change);
end
end
%-----------------End of function----------------------------------------------