MATLAB: How to communicate with Simulink from within a function

functionsend constantssim-commandsimulink

Hello everybody,
I do have communication problems with Simulink from within a function. I am used to use the sim()-command in m-files which are not declared as functions. Here I am able to define all constants inside my m-file, which are stored in the base workspace, and Simulink will use them (happily). But – and thats my problem – if I change the m-file to a function, calling this function from the 'Command Window', it isn't able to use these constants. This is the error message I get:
Error using runsim (line 43) Error due to multiple causes.
Caused by: Error using runsim (line 43) Error evaluating parameter 'Gain' in 'ball_beam/ball and beam model/Gain' Error using runsim (line 43) Undefined function or variable 'g'. Error using runsim (line 43) Error evaluating parameter 'Gain' in 'ball_beam/open-loop controller/Gain' Error using runsim (line 43) Undefined function or variable 'g'. Error using runsim (line 43) Error evaluating parameter 'Gain' in 'ball_beam/state feedback controller/K' Error using runsim (line 43) Undefined function or variable 'K'.
Well, next step is to ask the mathworks help :-): http://www.mathworks.de/de/help/simulink/ug/using-the-sim-command.html
Here it says: SimOut = sim('model', Parameters) Unfortunately, 'Parameters' seem to be simulation parameters like 'RapidAcceleratorParameterSets' which I found here: http://www.mathworks.de/de/help/simulink/slref/sim.html
So, how to solve this problem? Thank you for your help.

Best Answer

Please see my answer to a similar question here.