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
- Store the struct generated from the .M file into a .MAT file/.CSV file.
- In the MATLAB Function Block, read the parameter file using functions like “load” or “csvread” and extract the values into local variables.
- 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:
- You can use a single bus signal to input a struct-like parameter into a MATLAB Function block.
- 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) )
- Connect the output of the Bus Creator block to the input of your MATLAB Function block and access the struct fields inside the block.
- 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!