Help with exporting variables from function to workspace to then run in simulink file
21 views (last 30 days)
Show older comments
I have a function where I assign values to variables and then open a simulation and run it using those variables. I am using a function so that I can pick and choose which simulations I am running but although the simulations are within the function it doesn't recognise that they are there. Attached code below:
function IGBT_MOSFET = Sim_IGBT(n)
if n == 'On'
Vth_MOSFET = 3.8;
Vth_IGBT = 6.2;
Vf = 1.8;
R_on_MOSFET = 18e-3;
R_on_IGBT = 72.5;
E_on_MOSFET = 1100e-6;
E_off_MOSFET = 180e-6;
V_off_MOSFET = 800;
I_on_MOSFET = 42;
E_on_IGBT = 2.7e-3;
E_off_IGBT = 1.1e-3;
V_off_IGBT = 600;
I_on_IGBT = 40;
% open('Coursework_part_4_sim_IGBT.slx')
% open('Coursework_part_4_sim_MOSFET.slx')
% sim('Coursework_part_4_sim_IGBT.slx')
% sim('Coursework_part_4_sim_MOSFET.slx')
else
end
end
0 Comments
Answers (2)
Paul
on 3 Dec 2025 at 21:30
The "modern" way to do this with the sim command is to use a Simulink.SimulationInput as the input. See that doc page to get started, particularly the link to setVariable. Also, use the output argument form of sim.
0 Comments
Fangjun Jiang
on 3 Dec 2025 at 15:23
Edited: Fangjun Jiang
on 3 Dec 2025 at 15:24
You will need to use assignin('base','Vth_MOSFET',3.8) etc. to make this work.
Your current code assigns the variables and their values in the function workspace, while your Simulink model most likely uses the variables in the base workspace.
It will be inconvenient to have so many assignin() statements. The other way is to save these variables in a .m file and run evalin('base','Mfile')
0 Comments
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!