Home > m > helper > find_in_models.m

find_in_models

PURPOSE ^

FIND_IN_MODELS Search full block diagram hierarchy

SYNOPSIS ^

function out = find_in_models(modelname,varargin)

DESCRIPTION ^

FIND_IN_MODELS Search full block diagram hierarchy
  Wrapper for find_system which searches the full block diagram hierarchy,
  following library links and model references.  The contents of masked systems
  will be searched only if the LookUnderMasks option is supplied.

 out = find_in_models(modelname,varargin);
 out = find_in_models(modelhandle,varargin);

 The first input must be the name or numeric handle of a block diagram.
 Additional arguments are the same as those for find_system.  If a SearchDepth 
 is specified, this will be applied when searching each model in the hierarchy.

 The return value will be a cell array of strings if:
   1) the first input (modelname) is a string
   2) the "FindAll" option is absent or "off".
 Otherwise the return value will be a numeric array.

 However, note that if strings are returned, any block diagrams which are loaded
 during the search will be closed again to save memory.
 If numeric handles are returned then the block diagrams will be retained in
 memory, though they may not be visible.  Use:
    find_system('SearchDepth',0)
 to find the list of block diagrams which are in memory.

 Examples:
   To find all Gain blocks involved in the simulation:
     names = find_in_models('mymodel','BlockType','Gain');

   To find all lines with name "voltage":
     handles = find_in_models('mymodel','FindAll','on','Type','line','Name','voltage')

   To find all models in the simulation which have buffer reuse turned off:
     names = find_in_models('mymodel','SearchDepth',0,'BufferReuse','off')

 $Id: find_in_models.m,v 1.2 2008/03/03 09:11:02 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out = find_in_models(modelname,varargin)
0002 %FIND_IN_MODELS Search full block diagram hierarchy
0003 %  Wrapper for find_system which searches the full block diagram hierarchy,
0004 %  following library links and model references.  The contents of masked systems
0005 %  will be searched only if the LookUnderMasks option is supplied.
0006 %
0007 % out = find_in_models(modelname,varargin);
0008 % out = find_in_models(modelhandle,varargin);
0009 %
0010 % The first input must be the name or numeric handle of a block diagram.
0011 % Additional arguments are the same as those for find_system.  If a SearchDepth
0012 % is specified, this will be applied when searching each model in the hierarchy.
0013 %
0014 % The return value will be a cell array of strings if:
0015 %   1) the first input (modelname) is a string
0016 %   2) the "FindAll" option is absent or "off".
0017 % Otherwise the return value will be a numeric array.
0018 %
0019 % However, note that if strings are returned, any block diagrams which are loaded
0020 % during the search will be closed again to save memory.
0021 % If numeric handles are returned then the block diagrams will be retained in
0022 % memory, though they may not be visible.  Use:
0023 %    find_system('SearchDepth',0)
0024 % to find the list of block diagrams which are in memory.
0025 %
0026 % Examples:
0027 %   To find all Gain blocks involved in the simulation:
0028 %     names = find_in_models('mymodel','BlockType','Gain');
0029 %
0030 %   To find all lines with name "voltage":
0031 %     handles = find_in_models('mymodel','FindAll','on','Type','line','Name','voltage')
0032 %
0033 %   To find all models in the simulation which have buffer reuse turned off:
0034 %     names = find_in_models('mymodel','SearchDepth',0,'BufferReuse','off')
0035 %
0036 % $Id: find_in_models.m,v 1.2 2008/03/03 09:11:02 hewitson Exp $
0037 %
0038 
0039     if ~ischar(modelname) && ( ~isnumeric(modelname) || numel(modelname)~=1 )
0040         error('Simulink:find_in_models:BadModelSpecifier',...
0041             'First input must be single model name or handle');
0042     end
0043 
0044     % First guess at whether we need to close any block diagrams we load
0045     close_after_search = ischar(modelname);
0046 
0047     % The list of names of block diagrams currently in memory
0048     loaded_mdls = find_system('SearchDepth',0);
0049     
0050     % The list of block diagrams we've searched already, to avoid duplication
0051     % of effort where a block diagram appears in multiple locations in the hierarchy.
0052     searched_mdls = {};
0053     
0054     % Run the search.
0055     out = i_search(modelname,varargin{:});
0056     
0057     if close_after_search
0058         % Close any block diagrams which are now open but weren't open to
0059         % start with.  Since we closed models after we searched them, this will
0060         % only include libraries.
0061         now_loaded = find_system('SearchDepth',0);
0062         for k=1:numel(now_loaded)
0063             if ~ismember(now_loaded{k},loaded_mdls)
0064                 close_system(now_loaded{k},0);
0065             end
0066         end
0067     end
0068     
0069     %-----------------------------------------------
0070     % Peforms the search on the specified model and any models it references.
0071     function out = i_search(thismodel,varargin)
0072         
0073         % Check whether we need to load this model.
0074         if ischar(thismodel)
0075             isloaded = ismember(thismodel,loaded_mdls);
0076             if ~isloaded
0077                 load_system(thismodel);
0078             end
0079         end
0080         % Perform the search.
0081         out = find_system(thismodel,'FollowLinks','on',varargin{:});
0082         if close_after_search && isnumeric(out)
0083             % We supplied a name but got numeric handles back.  The caller must
0084             % have specified the "FindAll" option.  We can't close models after
0085             % searching them now, because that would make the handles invalid.
0086             close_after_search = false;
0087         end
0088         % Look for any Model References
0089         mdlrefs = find_mdlref_blocks(thismodel);
0090         if ~isempty(mdlrefs)
0091             % Find the names of the referenced models
0092             mdls = get_param(mdlrefs,'ModelName');
0093             others = cell(size(mdls));
0094             for i=1:numel(mdls)
0095                 submodel = mdls{i};
0096                 submodelname = submodel; % keep a copy of the name
0097                 if ~ismember(submodelname,searched_mdls)
0098                     % We haven't searched this model already.
0099                     if ~ischar(thismodel)
0100                         % We need to supply numeric handles to find_system.  First
0101                         % make sure that this model is loaded.
0102                         if ~ismember(submodel,loaded_mdls)
0103                             load_system(submodel);
0104                             loaded_mdls{end+1} = submodel; %#ok (growing in a loop, but hard to avoid)
0105                         end
0106                         % Now get the handle.
0107                         submodel = get_param(submodel,'Handle');
0108                     end
0109                     % Now search the model.
0110                     others{i} = i_search(submodel,varargin{:});
0111                     % Record the fact that we've already searched this model,
0112                     % so that we don't search it again.
0113                     searched_mdls{end+1} = submodelname; %#ok (growing in a loop, but hard to avoid)
0114                 end
0115             end
0116             % Combine results for this model and all the models it references.
0117             out = vertcat(out,others{:});
0118         end
0119         % If we're returning strings, close this model to free the memory.
0120         % This may leave libraries in memory, but that will save us loading them
0121         % again if they're used by another model, and we'll close them at the end.
0122         if close_after_search
0123             if ~isloaded
0124                 close_system(thismodel,0);
0125             end
0126         end
0127 
0128     end
0129 end
0130 
0131 
0132

Generated on Mon 31-Mar-2008 13:54:54 by m2html © 2003