comparison m-toolbox/classes/+utils/@prog/find_in_models.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function out = find_in_models(modelname,varargin)
2 %FIND_IN_MODELS Search full block diagram hierarchy
3 % Wrapper for find_system which searches the full block diagram hierarchy,
4 % following library links and model references. The contents of masked systems
5 % will be searched only if the LookUnderMasks option is supplied.
6 %
7 % out = find_in_models(modelname,varargin);
8 % out = find_in_models(modelhandle,varargin);
9 %
10 % The first input must be the name or numeric handle of a block diagram.
11 % Additional arguments are the same as those for find_system. If a SearchDepth
12 % is specified, this will be applied when searching each model in the hierarchy.
13 %
14 % The return value will be a cell array of strings if:
15 % 1) the first input (modelname) is a string
16 % 2) the "FindAll" option is absent or "off".
17 % Otherwise the return value will be a numeric array.
18 %
19 % However, note that if strings are returned, any block diagrams which are loaded
20 % during the search will be closed again to save memory.
21 % If numeric handles are returned then the block diagrams will be retained in
22 % memory, though they may not be visible. Use:
23 % find_system('SearchDepth',0)
24 % to find the list of block diagrams which are in memory.
25 %
26 % Examples:
27 % To find all Gain blocks involved in the simulation:
28 % names = find_in_models('mymodel','BlockType','Gain');
29 %
30 % To find all lines with name "voltage":
31 % handles = find_in_models('mymodel','FindAll','on','Type','line','Name','voltage')
32 %
33 % To find all models in the simulation which have buffer reuse turned off:
34 % names = find_in_models('mymodel','SearchDepth',0,'BufferReuse','off')
35 %
36 % $Id: find_in_models.m,v 1.1 2008/06/18 13:35:11 hewitson Exp $
37 %
38
39 if ~ischar(modelname) && ( ~isnumeric(modelname) || numel(modelname)~=1 )
40 error('Simulink:find_in_models:BadModelSpecifier',...
41 'First input must be single model name or handle');
42 end
43
44 % First guess at whether we need to close any block diagrams we load
45 close_after_search = ischar(modelname);
46
47 % The list of names of block diagrams currently in memory
48 loaded_mdls = find_system('SearchDepth',0);
49
50 % The list of block diagrams we've searched already, to avoid duplication
51 % of effort where a block diagram appears in multiple locations in the hierarchy.
52 searched_mdls = {};
53
54 % Run the search.
55 out = i_search(modelname,varargin{:});
56
57 if close_after_search
58 % Close any block diagrams which are now open but weren't open to
59 % start with. Since we closed models after we searched them, this will
60 % only include libraries.
61 now_loaded = find_system('SearchDepth',0);
62 for k=1:numel(now_loaded)
63 if ~ismember(now_loaded{k},loaded_mdls)
64 close_system(now_loaded{k},0);
65 end
66 end
67 end
68
69 %-----------------------------------------------
70 % Peforms the search on the specified model and any models it references.
71 function out = i_search(thismodel,varargin)
72
73 % Check whether we need to load this model.
74 if ischar(thismodel)
75 isloaded = ismember(thismodel,loaded_mdls);
76 if ~isloaded
77 load_system(thismodel);
78 end
79 end
80 % Perform the search.
81 out = find_system(thismodel,'FollowLinks','on',varargin{:});
82 if close_after_search && isnumeric(out)
83 % We supplied a name but got numeric handles back. The caller must
84 % have specified the "FindAll" option. We can't close models after
85 % searching them now, because that would make the handles invalid.
86 close_after_search = false;
87 end
88 % Look for any Model References
89 mdlrefs = find_mdlref_blocks(thismodel);
90 if ~isempty(mdlrefs)
91 % Find the names of the referenced models
92 mdls = get_param(mdlrefs,'ModelName');
93 others = cell(size(mdls));
94 for i=1:numel(mdls)
95 submodel = mdls{i};
96 submodelname = submodel; % keep a copy of the name
97 if ~ismember(submodelname,searched_mdls)
98 % We haven't searched this model already.
99 if ~ischar(thismodel)
100 % We need to supply numeric handles to find_system. First
101 % make sure that this model is loaded.
102 if ~ismember(submodel,loaded_mdls)
103 load_system(submodel);
104 loaded_mdls{end+1} = submodel; %#ok (growing in a loop, but hard to avoid)
105 end
106 % Now get the handle.
107 submodel = get_param(submodel,'Handle');
108 end
109 % Now search the model.
110 others{i} = i_search(submodel,varargin{:});
111 % Record the fact that we've already searched this model,
112 % so that we don't search it again.
113 searched_mdls{end+1} = submodelname; %#ok (growing in a loop, but hard to avoid)
114 end
115 end
116 % Combine results for this model and all the models it references.
117 out = vertcat(out,others{:});
118 end
119 % If we're returning strings, close this model to free the memory.
120 % This may leave libraries in memory, but that will save us loading them
121 % again if they're used by another model, and we'll close them at the end.
122 if close_after_search
123 if ~isloaded
124 close_system(thismodel,0);
125 end
126 end
127
128 end
129 end
130
131
132