Find Unused Fields in Simulink Structure Variables


Introduction

Simulink is a popular tool used for modeling and simulating dynamic systems. In Simulink, structure variables are used to store and organize data for easy access and manipulation. However, it is not uncommon for structure variables to contain unused fields, which can make code unnecessarily complex and hard to maintain. In this article, we will discuss how to find unused fields in Simulink structure variables and provide examples to demonstrate process.

What are Simulink Structure Variables?

In Simulink, a structure variable is a container that holds data in fields. Fields are individual elements of structure that store a single value, such as a scalar, vector, or matrix. structure variables are commonly used to organize data in Simulink models, including parameters, signals, and states.

For example, consider a simple Simulink model that models dynamics of a spring-mass-damper system. model contains three parameters, two signals, and one state, as shown below.

In this model, parameters, signals, and states can be stored in a structure variable, as shown below.

% Define a structure variable to store model data
model_data = struct('mass', 1, ...
                    'spring_constant', 10, ...
                    'damping_coefficient', 0.5, ...
                    'input_signal', [], ...
                    'output_signal', [], ...
                    'state', 0);

The fields of structure variable are defined using struct function, and data is stored in respective fields. input_signal and output_signal fields are left empty for now, as we will generate these signals later in model.

How to Find Unused Fields in Simulink Structure Variables

When creating a Simulink model, it is common to define a structure variable with many fields, some of which may be unused. Unused fields can make code unnecessarily complex and hard to maintain. Therefore, it is important to identify and remove these fields.

To find unused fields in Simulink structure variables, we can use MATLAB mlint function. mlint function is a code analyzer that checks code for potential issues and provides suggestions for improvement. In particular, mlint function can detect unused variables and fields in code.

To use mlint function, we need to save Simulink model as an M-file. M-file contains MATLAB code that generates Simulink model. To save model as an M-file, go to File -> Generate Code -> To File. Then, select M-file format and specify a file name and location.

Once M-file is generated, we can run mlint function on file using following command.

mlint('filename.m')

where filename.m is name of M-file.

The mlint function generates a report that highlights potential issues in code, including unused fields in structure variables. report provides suggestions for removing unused fields, as well as other improvements that can be made to code.

Example: Finding Unused Fields in a Simulink Structure Variable

To demonstrate how to find unused fields in Simulink structure variables, let us consider following Simulink model that models a simple feedback control system.

The model contains a plant subsystem, a controller subsystem, and a scope block for displaying output. plant subsystem models dynamics of a first-order system, while controller subsystem implements a proportional-integral (PI) controller.

To store model data, we can define a structure variable with following fields.

% Define a structure variable to store model data
model_data = struct('k', 1, ...
                    'tau', 0.1, ...
                    'setpoint', 1, ...
                    'kp', 1, ...
                    'ki', 0.1, ...
                    'output', [], ...
                    'input', []);

The k and tau fields are plant parameters, while setpoint, kp, and ki fields are controller parameters. output and input fields store output and input signals of system, respectively.

Let us now save model as an M-file and run mlint function on file.

% Save model as an M-file
model_file = 'feedback_control_system.m';
open_system('feedback_control_system');
rtwbuild('feedback_control_system', 'GenerateMakefile', 'off', 'MakefileName', 'makefile', 'BuildConfiguration', 'Faster Runs', 'CodeType', 'Real-Time Workshop');
movefile('feedback_control_system_ert_rtw/feedback_control_system.c', model_file, 'f');
rmdir('feedback_control_system_ert_rtw', 's');

% Run mlint function on M-file
mlint(model_file);

The mlint function generates following report.

Feedback control system.m 
      Unused variable or function 'model_data.input'.
      Unused variable or function 'model_data.output'.

The report indicates that input and output fields in model_data structure variable are unused. These fields can be removed to simplify code.

To remove unused fields, we can modify model_data structure variable as follows.

% Define a structure variable to store model data
model_data = struct('k', 1, ...
                    'tau', 0.1, ...
                    'setpoint', 1, ...
                    'kp', 1, ...
                    'ki', 0.1);

The input and output fields are removed from model_data structure variable.

In addition to mlint function, Simulink provides a built-in diagnostic tool called Model Advisor, which can be used to identify and resolve issues in Simulink models, including unused fields in structure variables. Model Advisor checks for model design errors and provides recommendations for improving model quality and performance.

To use Model Advisor, we can open Simulink model and select "Model Advisor" option from "Analysis" tab. Model Advisor window will open, and we can select "Unused signals, parameters, and states" check from list of checks. Model Advisor will then scan model for unused signals, parameters, and states, and provide recommendations for removing them.

It is important to note that while Model Advisor can help identify unused fields in structure variables, it may not be able to detect all issues in model. Therefore, it is recommended to use both Model Advisor and mlint function to ensure model is free from errors and unused fields.

Conclusion

In conclusion, Simulink structure variables are useful for organizing data in Simulink models. However, it is important to identify and remove unused fields in structure variables to avoid unnecessary complexity and improve maintainability. To find unused fields in Simulink structure variables, we can use MATLAB mlint function, which generates a report that highlights potential issues in code. By removing unused fields, we can simplify code and make it easier to maintain.

Updated on: 03-Mar-2023

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements