Joyom
Joyom

Reputation: 11

Using Simulink Block in MATLAB

I want to use a Simulink Block (MATLAB Function) like a function in MATLAB. This means that I already have a complex Simulink Model and I just want to use some of the blocks of the Model in Matlab.

Example for the Simulink Model

Upvotes: 1

Views: 1025

Answers (3)

parag
parag

Reputation: 25

Following things should be considered first:

- You have to run whole model (Simulink) when you want to use it as a function. Partial model cannot be run

You can do it in two ways based on following two conditions:

  1. Your model has the function, which is written in MATLAB script
  2. Your model has a function, which is created within simulink itself.(Like using Library-blocks)

Let us see one by one :

  1. If you have function created in MATLAB using, Interpreted MATLAB function (Interpreted MATLAB fcn) or just simple MATLAB function (fcn) , you can actually access the codes of these from your worksapce. Copy the code of these files and write your own function.

  2. If you have created the function in Simulink, and you do not have any other option but to use model itself, then do following:

      A. Define all input as variables in your model , using '**from workspace**'
      B. Define all your outputs using : '**To workspace**'
    
      C. Define your input variables in your workspace first.
             load('data.mat'); % this can be your input data
      D. define your model as a system in a script file
             sys = 'mysimulinkmodel'
      E. run model from your script file as follows:
             data_out = sim(sys) or directly, sim("mysimulinkmodel")
    

Once you run the model, you can access all the output data from data_out.

Hopefully you will be able to solve your problem with this.

Upvotes: 0

MichaelTr7
MichaelTr7

Reputation: 4767

The following is an example of a simple Simulink system that reads a variable from a MATLAB script, runs the model and exports the outputs into the workspace window. Automatically any variable names that are used in the model are pulled from the MATLAB script where the model was called/executed from. The outputs are obtained by using a sink block allowing a part of the model to be exported directly to the workspace panel. The system below takes the input adds 2 and then multiplies by 2. Similarly this setup also works for fcn, function blocks that are user defined.

Simulink Model

Simulink Model Disgram


MATLAB Script

%Variables used in model%
Input = 5;

%Calling model and returning results from Simulink model%
Results = sim('Model.slx');
Results.Output.Data

Ran using MATLAB R2019b

Upvotes: 0

Mansour Torabi
Mansour Torabi

Reputation: 421

If you want to run a simulink model from MATLAB, use the command sim('YourSimulinkModel.slx'), And in your simulink model, use the block To Workspace (address: simulink/Sinks/To Workspace). to export the results of simulink model to the MATLAB Workspace after execution.

Upvotes: 1

Related Questions