How do I call Simulink from Within Matlab then go back to the M-file software?

5 views (last 30 days)
Alright, I need to call a Simulink file from an M file as though it were a subroutine. How do I call the Simulink file from within an M-file then return control to the M-file? Again, I would like to use the Simulink file like a subroutine. Can I do that? We'll say the Simulink file that I want to use is called PlaneCalcs and I don't know what the extension on that should be, which should give you some idea of how much of a newbie I am.
Can you also provide me with how I would transfer data to the Simulink file (call it "PlaneCalcs") then load that data into an input that would then feed an integrator. I've been told that I can use a multiplexer to supply the input to the Simulink integrator.
Please keep your response as basic as possible. Too much data will overload my mind and make your answer useless. I have NO SIMULINK background, so please don't tell me to use fancy function names. I'll probably need to multiply the inputs by a matrix. Again, can you tell me what to use to do this multiplication?
Also, how do I write a Simulink file and name it PlaneCalcs, so I can access it via the M-file?

Accepted Answer

Bhanu Prakash
Bhanu Prakash on 13 Jul 2024
Edited: Bhanu Prakash on 13 Jul 2024
Hi DJ,
Here is a breakdown of all the things you have asked:
1. Create and call Simulink model
To create and call a SImulink model 'PlaneCalcs' from MATLAB, you can use the function 'new_system' and 'open_system' as follows:
new_system('PlaneCalcs');
open_system('PlaneCalcs');
2. Add Simulink blocks and set their parameters
To add new Simulink blocks to the model 'PlaneCalcs' and set its parameters, you can use the funcitons 'add_block' and 'set_param'. You can add the multiplexer and set the number of inputs as follows:
add_block('simulink/Signal Routing/Mux', ['PlaneCalcs' '/Mux']);
set_param(['PlaneCalcs' '/Mux'], 'Inputs', '1');
To add a block to the model using 'add_block' you need to know the library pat of the block that you want to add. To know the process of finding a block's path, you can refer to the 'Add Block to Model from Library' section in the following documentation:
3. Connecting the blocks
To connect the block in the model, you can use the 'add_line' funciton in MATLAB. To connect multiplexer with integrator, you can use the below command:
add_line('PlaneCalcs', 'Mux/1', 'Integrator/1');
4. Save and close the model
Once you have added all the blocks and connceted them, you can save and close the models using the commands below:
save_system('PlaneCalcs');
close_system('PlaneCalcs');
5. Load input data and simulate the model
To simulate a model, you need to specify the model using 'Simulink.SimulationInput' and then pass the Input data to the model using 'setVariable' . Here is a sample code for the same:
inputData = [1; 2]; % Input data
load_system('PlaneCalcs'); % loading the model
simInput = Simulink.SimulationInput('PlaneCalcs'); % specify the model for simulation
simInput = simInput.setVariable('inputData', inputData); % pass the input data
simOutput = sim(simInput); % run the simulation
Please note that these code snippets are for you to understand the workflow involved and this is not the entire code for your usecase.
For more information regarding the funcitons mentioend, you can refer to the following documentation:
Additionally, I also suggest you go through the Simulink Onramp to get hands-on experience with using Simulink:
Hope this helps!

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!