In addition to the S-Function , there are other ways to pass data from Matlab to Simulink:
1) Initializing the variables in Matlab:
Instead of calling a M-Function inside Simulink, we can type the variables at the Matlab command line or to run a program in Matlab before running the model in Simulink.
Consider the following example: we want to create a model in Simulink to multiply a constant value with a certain gain. We ca pass this gain as a variable from Matlab workspace. So, the following block diagram should be build in Simulink:
The gain K will be passed from the Matlab workspace. Let’s say that the gain is equal to 50.
To use this gain, you first have to define the variable K in Matlab with a value of 50. This is shown in the next figure:
Now, if we run our Simulink model, the output will 50:
We can define any number of variables in the Matlab workspace and just use the variable names in the Simulink model.
We can also write a Matlab M-file just to define the variables that we want. For example, the following program, called gains .
m , define three variables, K1, K2 and K3:
Now, to use these gains in Simulink, we have to run this program in Matlab first:
To see these variables in Simulink, just create the following simple model and run it:
2) Using a Subsystem and a M-File to initialize the variables:
As your model increases in size and complexity, you can simplify it by grouping blocks into
Subsystems. Using Subsystems has these advantages:
•It helps reduce the number of blocks displayed in your model window.
•It allows you to keep functionally related blocks together.
•It enables you to establish a hierarchical block diagram, where a Subsystem block is on one layer and the blocks that make up the subsystem are on another.
You can create a Subsystem in two ways:
•Add a Subsystem block to your model, then open that block and add the blocks it contains to the Subsystem window.
•Add the blocks that make up the Subsystem, then group those blocks into a Subsystem.
We are going to insert a Subsystem block to a new model and use this Subsystem to initialize our parameters. To do that, we have to select the Subsystem Library and select the Subsystem block.
Then, drag this block to your model:
Now, in this example, we just want to use the Subsystem to initialize our variables. So, we do not need any input or output from our Subsystem. So, double-click on the Subsystem block and delete the input and output connectors. Close the Subsystem.
Now, we are going to call the same Matlab file we created: gains .
m . To do that, click with the mouse right button on the subsystem block and select Block proprieties… On the Open function field type the name of the file you want to open (in this case: gains ). The next figure shows that:
Now, incorporate in your model 3 constant blocks, 3 gain blocks and 3 display blocks. Now, to run this system, we have to, first, double-click on the Subsystem block, in order to initialize the variables. So, do that and then run the model. You should have the following result: