slreportgen.finder.ModelVariableFinder class

Package: slreportgen.finder
Superclasses: mlreportgen.finder.Finder

Finds variables used by a Simulink model

Description

Find variables used by a Simulink® model.

The slreportgen.finder.ModelVariableFinder class is a handle class.

Class Attributes

HandleCompatible
true

For information on class attributes, see Class Attributes (MATLAB).

Creation

Description

example

finder = slreportgen.finder.ModelVariableFinder(container) creates a finder that finds variables used in the specified container, which can be a Simulink model or subsystem. See the Container property. You can constrain the search by setting the properties of the finder. Use the methods of the finder to perform the search.

Note

This finder provides two ways to get search results:

  1. To return the search results as an array, use the find method. Add the results directly to a report or process the results in a for loop.

  2. To iterate through the results one at a time, use the hasNext and next methods in a while loop.

Neither option has a performance advantage.

finder = slreportgen.finder.ModelVariableFinder(Name,Value) sets properties using name-value pairs. You can specify multiple name-value pair arguments in any order. Enclose each property name in single or double quotes.

Properties

expand all

Model or subsystem to search, specified as a string scalar or character vector that contains the path to the model or subsystem, or as a handle to the model or subsystem.

Regular expression matching, specified as "off" or "on". If Regexp is "off", regular expression matching is not enabled. If Regexp is "on", regular expression matching is enabled for the values of the Name, SourceType, and Users properties. For example, this code finds variables that start with vehicle.

finder = slreportgen.finder.ModelVariableFinder('sf_car');
finder.Regexp = "on";
finder.Name = "^vehicle";

See Regular Expressions (MATLAB).

Compile status, specified as one of the values in the table.

ValueDescription
"compiled"Get up-to-date results by compiling models before the search. (default)
"cached"Get results more quickly by using data cached during the previous compilation.

Whether to search for variables in referenced models, specified as one of the values in the table.

ValueDescription
"on"Search for variables in referenced models. (default)
"off"Do not search for variables in referenced models.

Name of variable to search for, specified as a character vector or string scalar. If the Regexp property is set to "on", the value of Name can be a regular expression. If the Name property is empty, the finder does not search based on the variable name.

Example: "vehicledata"

Example: "^vehicle"

Source of variable definitions, specified as one of these values:

  • "base workspace"

  • "model workspace"

  • "mask workspace"

  • "data dictionary"

If you set SourceType, the finder returns variables only from the specified source. If the Regexp property is set to "on", the value of SourceType can be a regular expression. If the SourceType property is empty, the finder does not filter the search results by the source.

Example: finder.SourceType = "model workspace" returns all variables defined in the model workspace.

Example: finder.SourceType = "(base|mask) workspace" returns all variables defined in the base workspace or the mask workspace if the Regexp property is set to "On".

Example: finder.SourceType = "\w* workspace" returns all variables defined in the base, mask, or model workspace if the Regexp property is set to "On".

Names of blocks to search for variables. Specify one block as a character vector or string scalar. Specify multiple blocks as an array of character vectors or a string array. The finder returns variables used by one or more of the specified blocks. If you do not set the Users property, the finder searches the entire model or subsystem. If the Regexp property is set to true, you can set the Users property to a regular expression.

For example, to find all variables in MyModel that are used by either the Gain1 block or the Gain2 block, you can specify both blocks in the Users property.

myFinder.Users = ["myModel/Gain1", "myModel/Gain2"];

Alternatively, you can use a regular expression that matches both block names.

myFinder.Regexp = "on";
myFinder.Users = "Gain(1|2)";

Whether to search for variables in masked subsystems, specified as one of the values in the table.

ValueDescription
"all"Search for variables in masked subsystems. (default)
"none"Do not search for variables in masked subsystems.

Whether to follow library links when searching for variables, specified as one of the values in the table.

ValueDescription
"on"Follow links into library blocks. Library links are treated as subsystems. (default)
"off"Do not follow links into library blocks. Library links are treated as blocks.

Whether to include variables of inactive variant systems, specified as one of the values in the table.

ValueDescription
"off"Do not include inactive variants. (default)
"on"

Include inactive variants if all of the following conditions are true:

  • The SearchReferencedModels property is set to "on".

  • The 'Analyze all choices during update diagram and generate preprocessor conditionals' configuration parameter is selected for the variant subsystem.

Properties of Simulink.VariableUsage objects to find, specified as a cell array of name-value pairs. The finder returns only variables whose associated Simulink.VariableUsage object has the specified property values.

Example: finder.Properties = {'SourceType', 'base workspace'}

Methods

expand all

Examples

collapse all

Find the variables in a model and add the results directly to a report. Specify that the finder includes variables in masked systems.

% Create a Simulink Report
rpt = slreportgen.report.Report("MyReport","pdf");

% Create a Chapter
chapter = mlreportgen.report.Chapter();
chapter.Title = "Model Variable Finder Example";

% Load the model
model_name = "sf_car";
load_system(model_name)

% Create a variable finder and set its properties
finder = slreportgen.finder.ModelVariableFinder(model_name);
finder.LookUnderMasks = "all";

% Find variables used by the model
results = find(finder);

% Add the results to the chapter
add(chapter,results);

% Add chapter to the report
add(rpt,chapter);

% Close the report and open the viewer
close(rpt);
rptview(rpt);

Customize the formatting of model variables in a report by iterating through the search results and setting properties of the model variable reporter for each result.

% Create a Report
rpt = slreportgen.report.Report("MyReport","pdf");

% Create a Chapter
chapter = mlreportgen.report.Chapter();
chapter.Title = "Model Variable Reporter Example";

% Load the model
model_name = "sf_car";
load_system(model_name);

% Find the variables in the model
finder = slreportgen.finder.ModelVariableFinder(model_name);

while hasNext(finder)
    result = next(finder);
    
    % Get the ModelVariable reporter for the result
    % Customize the formatting of numbers
    reporter = getReporter(result);
    reporter.NumericFormat = "%.4f";
    
    % Add the reporter to the chapter
    add(chapter,reporter);
end
% Add chapter to the report
add(rpt,chapter);

% Close the report and open the viewer
close(rpt);
rptview(rpt);

Introduced in R2019b