Change Simulink parameters in Matlab function

2 visualizaciones (últimos 30 días)
Frederik Rentzsch
Frederik Rentzsch el 29 de Ag. de 2020
Editada: Kilian Lasch el 14 de En. de 2022
I have a Simulink model (SM) and want to write a Matlab function (MF) (to which I pass some parameters of SM), in which I would like to change some parameters of SM and simulate it. I know how to change SM's parameters from workspace using set_param and Simulink.Parameter but I fail to make it work inside MF. As an example, suppose you have created SM and inside there is a Gain-Block called Gbl. I would like to use MF to update/change the value of Gbl. Calling MF with the variables var1 and var2 (see the code below) doesn't work (either var1 and var2 are in workspace, then MF will not update them or if they are not, there will be an error saying that var1 doesn't exist)
function MF(var1,var2)
set_param('SM/Gbl','Gain','2*var1*var2') % this doesn't work as intended inside a function
simOut = sim('SM','SaveOutput','on','OutputSaveName','yout','SaveFormat','Dataset');
yout = simOut.get('yout');
How can you make it work, so var1 and var2 get updated to the values passed to MF?
Further considerations: I would like to use MF for some optimization, so the changing of the parameters of SM should be very fast. Any ideas how to do that?
  1 comentario
Kilian Lasch
Kilian Lasch el 14 de En. de 2022
Editada: Kilian Lasch el 14 de En. de 2022
Have you found a solution or workaround for the problem? I'm currently facing the same error.

Iniciar sesión para comentar.

Respuestas (1)

Peter O
Peter O el 29 de Ag. de 2020
Simulink will look for variables in MATLAB's base workspace by default. Since var1 and var2 are defined in the function's workspace, it won't find them and you'll get that error. To have it use the workspace of a function, you need to pass an additional argument to the sim function.
Try:
sim('SM','SaveOutput','on','OutputSaveName','yout','SaveFormat','Dataset','SrcWorkspace','current')
There's a couple restrictions if you're running in parfor loops or using the Accelerator mode, but there are workarounds if those conditions apply.

Categorías

Más información sobre Schedule Model Components en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by