How do I run a model in Simulink from my MATLAB function where my Simulink parameters are defined in this function?

Illustration
Rebeccashah - 2020-07-31T12:00:32+00:00
Question: How do I run a model in Simulink from my MATLAB function where my Simulink parameters are defined in this function?

I want to simulate a model using the SIM command from inside a MATLAB function. My model has mask parameters that are variables defined in my function, fcn_name.m. I know that Simulink uses the variables stored in the base workspace. Is there a way to simulate my model from inside my function without creating variables in the base workspace?

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

When using variable mask parameters in Simulink, the base workspace is the default source workspace of Simulink. However, by using the SIMSET command, this workspace can be changed. SIM is then used with this options structure created by SIMSET. The following is an example on how to do this.

 

options = simset('SrcWorkspace','current');

sim('modelname',[],options)

This will change the source workspace of Simulink to the current workspace which is your function's workspace. Consult the currrent product documentation for more information about SIMSET.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!