Method to input multiple parameters to a MATLAB function block that does not result in a long compile time?

14 views (last 30 days)
I have a Simulink model that contains a MATLAB function block with a large amount of code. Whenever I modify the content of this function block, the model takes about 5-10 minutes to compile the next time I run it. This function block has a structure parameter; the fields of the struct are defined using a .m file that is run using the InitFcn callback. I have noticed that if I modify the value of any field in the struct (using the .m file, before the simulation begins to run) the model will also take approximately 5-10 minutes to compile on the next run, even if I have not otherwise modified the code within the MATLAB function block. Is there an alternative method to input multiple parameters (the struct fields) into a MATLAB function block that will not result in the large compile times? The struct I am using has many fields (around 100).

Answers (1)

T.Nikhil kumar
T.Nikhil kumar on 22 Sep 2023
Hey Ted!
As per my understanding, you are accessing a struct parameter from a .m” file via your Simulink model’s InitFcn to use it in a MATLAB Function Block in your model. There is a very large compilation time for the model when you modify fields of the struct using the “.M” file and you wish to know about alternative methods to pass the struct parameter that will have smaller compilation times on modification.
Compiling such Simulink models with complex MATLAB function blocks may take a lot of time, especially if there are many parameters to initialize. Here are a couple of possible ways, that I would suggest, to access struct parameters with reduced compilation time on modification. Note that limitation of usage of these methods will depend on your exact code and the type of entities in your struct’s fields.
Method 1: Loading Parameters in run time from a file
  1. Store the struct generated from the .M file into a .MAT file/.CSV file.
  2. In the MATLAB Function Block, read the parameter file using functions like load or csvread and extract the values into local variables.
  3. Whenever there are any modifications to be made, save the modified struct in the .MAT file/.CSV file again and recompile the model.
Method 2: Using Simulink.Bus object:
  1. You can use a single bus signal to input a struct-like parameter into a MATLAB Function block.
  2. For this you would need to create a bus object using the createObject function in MATLAB (Refer Create Simulink.Bus objects from blocks or MATLAB structures - MATLAB Simulink.Bus.createObject (mathworks.com) ) and then create a bus creator block on Simulink to create a new bus signal using the bus object you defined in MATLAB. (Refer Group input signals or messages into bus - Simulink (mathworks.com) )
  3. Connect the output of the Bus Creator block to the input of your MATLAB Function block and access the struct fields inside the block.
  4. Modification to the bus object can be done using the “Type Editor interactively (Refer Create, modify, and manage types, such as bus objects - MATLAB (mathworks.com) )
Additionally, this may also be achieved using a mask on the MATLAB Function block and having the struct as a mask parameter. Refer to the following documentation for more information on this method.
Hope this helps!

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!