Is it possible to pause & restart a Simulink model from within c-mex s-function code?

I have c-mex s-function code that specifies the behavior of a block within my Simulink model. When a certain condition is met within the c-mex s-function code, a TerminateSimulation variable is set to 1, and the c-mex s-function code sends this output signal to a Simulink Stop block, which stops the model from running. 

I’ve recently discovered that it’s going to be problematic for me to *stop* the Simulink model from running, between each time I need it to execute (because certain variable values in the workspace that I need to persist, end up being reset). What I now want to do, is to *pause* the Simulink model from my c-mex s-function code, and then restart it after a few seconds have passed. 

I searched through the Simulink block library, but unlike the existing Stop block, I don’t see any Run/Play or Pause blocks. Is there going to be any way for me to pause and then restart my Simulink model from within my c-mex s-function code? 

(side question: I see on one documentation page that “When you stop or pause a model, all existing handles for run-time objects become empty.” --> do these “handles” include Dwork vectors? If so, then maybe this whole idea of pausing, rather than stopping, the Simulink model, won’t work anyway…)

I see that the mdlSimStatusChange() function exists:

…but that function is only useful *after* the Simulink model has already been paused or resumed; I don’t see any clues here about how to actually pause or resume the model… 

Thanks in advance for your help! 
kmj.work (178)
4/10/2012 12:33:13 AM
comp.soft-sys.matlab 211266 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies

Similar Articles

[PageSpeed] 38

A follow-up question to my original post:

If, for some reason, it's impossible to pause & restart the Simulink model from within my c-mex s-function code, and/or if the Pause process clears the c-mex s-function variable that I require to persist, is there any other way to accomplish a functional pause from within my c-mex s-function code? Trying to call pause(5) just results in a compiler error message, as apparently pause() isn't a legitimate function in c-mex s-functions. I could always implement a dummy loop that executes a few million times before my other code resumes, but it seems like there should be a more elegant way to accomplish a c-mex s-function code pause, while keeping my variable values intact. 

Thanks for any suggestions anyone has. 
kmj.work (178)
4/10/2012 12:48:12 AM
"Kate J." wrote in message <jm000c$dv6$1@newscl01ah.mathworks.com>...
> pause & restart the Simulink model from within my c-mex s-function code

While it is not something I have done, this thread appears helpful,
4/10/2012 6:25:26 PM
Thanks for the link, Kate! Yeah, I had found that solution previously; I *believe* that the set_param() function is only valid within Matlab scripts and workspace, and not c-mex s-functions. However, I am considering trying to use these set_param() calls from my gui m-file script, *if* pausing the Simulink model allows the variables to persist when the model is restarted.

Thanks again for your response! 
kmj.work (178)
4/10/2012 11:05:13 PM
As with your attempt to call pause (and another post where you had problems calling "clear all"), calling set_param or any other MATLAB function from within C code (whether it's a MATLAB mex file or a c-mex S-Function) will never work.
One is C code the other is m-code.

To call MATLAB functions from within C code you need to use the API function mexCallMATLAB.

As for your pause problem (and again going on what you've said in other threads) it seems to be that you need to rethink your whole simulation set-up.
The Simulink model should be set to work with one data set with each set being run in a loop, not multiple data sets with some sort of pause between them so that data can be written/read from the MATLAB workspace.

phil986 (332)
4/11/2012 2:33:20 PM
Thanks for all of your helpful input, Phil, on this and the other threads.

I agree that attempting to pause my Simulink model in between data sets isn't going to work. Your suggestion to use a Matlab loop sounds like the way to go, although I'm considering just processing each data set in its own separate directory, to avoid a variety of issues. 

Thanks again! :) 
kmj.work (178)
4/11/2012 5:48:14 PM