Change Simulink parameters in Matlab function

조회 수: 2 (최근 30일)
Frederik Rentzsch
Frederik Rentzsch 2020년 8월 29일
편집: Kilian Lasch 2022년 1월 14일
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
Kilian Lasch
Kilian Lasch 2022년 1월 14일
편집: Kilian Lasch 2022년 1월 14일
Have you found a solution or workaround for the problem? I'm currently facing the same error.

댓글을 달려면 로그인하십시오.

답변 (1개)

Peter O
Peter O 2020년 8월 29일
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.

카테고리

Help CenterFile Exchange에서 Schedule Model Components에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by