Help with exporting variables from function to workspace to then run in simulink file

25 visualizaciones (últimos 30 días)
Simeon
Simeon el 3 de Dic. de 2025 a las 14:59
Respondida: Paul el 3 de Dic. de 2025 a las 21:30
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

Respuestas (2)

Paul
Paul el 3 de Dic. de 2025 a las 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.

Fangjun Jiang
Fangjun Jiang el 3 de Dic. de 2025 a las 15:23
Editada: Fangjun Jiang el 3 de Dic. de 2025 a las 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')

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by