- set_param: https://www.mathworks.com/help/simulink/slref/set_param.html
- add_line: https://www.mathworks.com/help/simulink/slref/add_line.html
- Simulink.SimulationInput: https://www.mathworks.com/help/simulink/slref/simulink.simulationinput.html
- setVariable: https://www.mathworks.com/help/simulink/slref/simulink.data.datasourceworkspace.setvariable.html
- sim: https://www.mathworks.com/help/simulink/slref/sim.html
How do I call Simulink from Within Matlab then go back to the M-file software?
4 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
Bhanu Prakash
2024 年 7 月 13 日
編集済み: Bhanu Prakash
2024 年 7 月 13 日
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!
1 件のコメント
Walter Roberson
2024 年 7 月 13 日
I would add to this that sometimes it is useful to set a StopTime for the simulation;
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Programmatic Model Editing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!