Simulink Model Stuck Initializing

44 views (last 30 days)
I just compiled a C MEX function and am trying to create an S-Function using it. However, after entering it into the S-Function block and pressing play, the model goes into "Initializing" mode and just stays like that. I have left it on for hours with no progress whatsoever. And I have to close MATLAB in order to get it to stop. What does this mean?

Accepted Answer

Benjamin Bokser
Benjamin Bokser on 20 Aug 2017
Okay, I finally figured it out. It was actually a code issue. For one thing, I was nesting functions within mdlOutputs, which I should have known better than to do since you can't nest functions in C. Second of all, I had the block inputs redefined as inputs to the objective function:
double myfunc(unsigned n, const double *x, double *grad, void *my_func_data, double xi, double yi)
Which was overwriting the actual input values with nothing. The funny thing is in my original code I didn't have that mistake; I must have added that to the code in MATLAB on a whim while debugging and forgotten to remove it.
See attached for the fixed version.

More Answers (1)

Joe Murphy
Joe Murphy on 16 Aug 2017
Hello Benjamin,
There could be a number of different things leading to this issue. In order to determine the cause of the issue, it will be important to get an understanding of whether or not is the S-function alone as a cause, and what inside the S-function code might be the cause. If you are able to, please provide your model, or a reduced example version of your model that still produces the issue, and all files necessary to run it as attachments.
If you cannot provide your model in any capacity, please reduce the components in your model and code in your S-function to their most simple versions that still cause the issue. Afterwards, please describe in as much detail the workflow you are using to generate the C MEX function and build the model, as well as the functions being used in your code and the components involved in your model.
Joe
  2 Comments
Benjamin Bokser
Benjamin Bokser on 17 Aug 2017
Hello Joe,
Attached are the .c, .mexa64 and .slx files involved. The code is heavily based off of the NLopt tutorial and I have also included the original .c code ("arminimize.c") for reference. You would of course have to install NLopt in order to test it. There aren't any other dependencies, although I am using Ubuntu 14.04 and gcc if that helps.
Basically, I followed the NLopt tutorial, made my own modifications, confirmed that my new code compiled outside of Matlab with gcc and worked properly, and then proceeded to attempt to adapt it to Simulink with an S-Function builder block. I had issues adding the library path to the S-Function builder, so I took the C code and wrapper function generated by the builder block, combined them, and compiled it as a C MEX function. I then attempted to use the S-function block, and that's when I discovered that the model would get stuck initializing.
Benjamin Bokser
Benjamin Bokser on 17 Aug 2017
Well, I simplified the code as much as I could while still causing the issue. See attached. The difference between getting stuck and not getting stuck is in this line:
if (nlopt_optimize(opt, x, &minf) >= 0) {
I tried replacing "&minf" with "NULL" and the model ran immediately, but outputted zeros, as I believe specifying "NULL" may cause the optimization function to not actually run.
But I know that the original code works, so the issue must lie in the interface between NLopt and Simulink...

Sign in to comment.

Categories

Find more on Event Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!