-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean_models.m
More file actions
111 lines (92 loc) · 4.46 KB
/
clean_models.m
File metadata and controls
111 lines (92 loc) · 4.46 KB
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
function clean_models()
% Load constants
while 1
bdclose all;
clear all;
set(0, 'DefaultFigureVisible', 'off');
warning('off','all')
LOCATION = Helper.cfg().models_path;
NEWLOCATION = Helper.cfg().tame_models_path;
if ~exist(NEWLOCATION, 'dir')
mkdir(NEWLOCATION); % Create the new location if it doesn't exist
end
% State file to track last processed model
state_file = fullfile(NEWLOCATION, 'last_processed_model.txt');
% Recursively get all .slx and .mdl files
files_slx = dir(fullfile(LOCATION, '**', '*.slx'));
files_mdl = dir(fullfile(LOCATION, '**', '*.mdl'));
files = [files_slx; files_mdl];
total_models = numel(files);
% Read state file to skip already processed models
last_processed = 0;
if exist(state_file, 'file')
fid = fopen(state_file, 'r');
last_processed = str2double(fgetl(fid));
fclose(fid);
end
% Process models
for i = 1:total_models
model_file = files(i).name;
model_path = fullfile(files(i).folder, model_file);
% Skip already processed models
if i <= last_processed
continue;
end
try
fprintf('Cleaning model %d of %d: %s\n', i, total_models, model_path);
% Update state file
fid = fopen(state_file, 'w');
fprintf(fid, '%i', i);
fclose(fid);
% Load model
model = load_system(model_path);
set_param(model, 'Lock', 'off');
% Remove all callback functions for the model
model_functions = {'PreLoadFcn' 'PostLoadFcn' 'InitFcn' 'PreStartFcn' 'StartFcn' 'PauseFcn' 'StopFcn' 'ContinueFcn' 'CloseFcn' 'CleanupFcn' 'PreSaveFcn' 'PostSaveFcn' 'SetupFcn' 'CustomCommentsFcn' 'DefineNamingFcn' 'ParamNamingFcn' 'SignalNamingFcn'};
for m = 1:length(model_functions)
set_param(model, model_functions{m}, '');
end
lock_links_masks = {'LinkStatus', 'none'; 'Lock', 'off'; 'LockLinksToLibrary', 'off'; 'Permissions', 'ReadWrite'; 'LinkStatus', 'none'; 'Lock', 'off'; 'LockLinksToLibrary', 'off'; 'Mask', 'off'};
block_functions = {'OpenFcn' 'LoadFcn' 'MoveFcn' 'NameChangeFcn' 'PreCopyFcn' 'CopyFcn' 'ClipboardFcn' 'PreDeleteFcn' 'DeleteFcn' 'DestroyFcn' 'UndoDeleteFcn' 'InitFcn' 'StartFcn' 'ContinueFcn' 'PauseFcn' 'StopFcn' 'PreSaveFcn' 'PostSaveFcn' 'CloseFcn' 'ModelCloseFcn', 'DeleteChildFcn', 'ErrorFcn', 'ParentCloseFcn'};
blocks = Helper.find_elements(model);
for j = 1:numel(blocks)
for ll = 1:size(lock_links_masks, 1)
try
set_param(blocks(j), lock_links_masks{ll, 1}, lock_links_masks{ll, 2});
catch ME
end
end
for bf = 1:length(block_functions)
try
set_param(blocks(j), block_functions{bf}, '');
catch ME
end
end
end
ports = find_system(model, 'FindAll','on', 'LookUnderMasks','on', 'FollowLinks','on', 'IncludeCommented', 'on', 'Variants','AllVariants', 'Type', 'Port');
for p = 1:numel(ports)
try
set_param(ports(p), 'ConnectionCallback', '');
catch ME
end
end
% Save cleaned model
[~, name, ext] = fileparts(model_file);
new_name = sprintf('m%d%s%s', i, name, ext);
save_system(model, fullfile(NEWLOCATION, new_name), 'BreakLinks', true);
close_system(model);
bdclose all;
catch ME
if ~startsWith(ME.message, "Failed to load library") && ~startsWith(ME.message, "Cannot find library")
fprintf('Model %d failed: %s\nReason: %s\n', i, model_path, ME.message);
end
bdclose all;
break
end
end
if i == total_models
break
end
end
fprintf('Processing complete.\n');
end