Forums

How many c mex s-functions can a mdl file support?

Started by yuemuping January 2, 2008
Hi, Everyone~ Happy New Year~~~

I have a weird question.

I've wrote several c mex s-functions in simulink. When I ran
the mdl file with one c mex s-function per a model, it worked
pretty well. However, when I put several c mex s-functions in
one mdl file and ran it, the result went mad. Does it have
something to do with the configuration settings? Could
anyone please tell me how can I fix the problem?

Thanks very much!

Miaoxiao
How many is several? Our models use over a dozen mex files in a single model.

One thing to be aware of is that you need to avoid using global variables in your mexfile if you want to have more than one instance running in a single model. Could this be the cause of your problem?

Sasha Case

Hi, Everyone~ Happy New Year~~~
>
>I have a weird question.
>
>I've wrote several c mex s-functions in simulink. When I ran
>the mdl file with one c mex s-function per a model, it worked
>pretty well. However, when I put several c mex s-functions in
>one mdl file and ran it, the result went mad. Does it have
>something to do with the configuration settings? Could
>anyone please tell me how can I fix the problem?
>
>Thanks very much!
>
>Miaoxiao
>
--- In m..., scase@... wrote:
>
> How many is several? Our models use over a dozen mex
files in a single model.
>
> One thing to be aware of is that you need to avoid using
global variables in your mexfile if you want to have more than
one instance running in a single model. Could this be the
cause of your problem?
>
> Sasha Case

------------------------------
Thanks very much!!!

Actually, the problem does have something to do with the
global variables as you reminded. When I submitted the
question, I wrote the code as follows:

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
InputRealPtrsType uPtrs_01 ssGetInputPortRealSignalPtrs(S,0);
real_T *y_01 = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);

float uk_01,ek_01;

for (i=0; i
if (i==1)
{y_01[0] = 1/0.116*(*uPtrs_01[0]);
uk_01 = y_01[0];
ek_01=*uPtrs_01[0];}
else
{y_01[0] = 1/0.116*(*uPtrs_01[0])-1/0.116*ek_01+0.1/
0.116*uk_01;
uk_01 = y_01[0];
ek_01=*uPtrs_01[0];}
}
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Without initializing the two viriables"uk_1, ek_01"

Then, I tried to initialize them, but problem came.

1. Tried to initialize the two variables as "0". And then, I found
that the simulink result went mad even under the situation of
one c mex s function per one mdl file. However, fortunatlly,
the mad result is actually the same as the result when I put
several c mex s functions together in one mdl file, as I
mentioned in Message 1, so I can make sure that's where the
problem hides.

2. Tried to initialize the two variables as "0.00", while the
result is the same as mentioned above.

3. After some tests, I found that, when I initialize the two
variables as "0", they will keep the value during the whole
program running, without changing as the code
"ek_01=*uPtrs_01[0] " or "uk_01 = y_01[0]" going.

4. When I left the two variables uninitializing, the result of one
s function per one mdl file ran well again....

That's weird. I did another test as follows,

changed the code of one of my s functions' static
void mdlOutputs from:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
float uk_003=0.00;

for (i=0; i if (i==1)
{y_003[0] = 0.016/0.046*(*uPtrs_003[0]);
uk_003 = y_003[0];}
else
{y_003[0] = 0.016/
0.046*(*uPtrs_003[0])+0.03/0.046*uk_003;
uk_003 = y_003[0];}
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
into:
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

for (i=0; i if (i==1)
{y_003[0] = 0.016/0.046*(*uPtrs_003[0]);}
else
{y_003[0] = 0.016/
0.046*(*uPtrs_003[0])+0.03/0.046*y_003[0];}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

And then, the result turned to be what I want. It seems that
once I set an internal variable, it works mad......

Finally, when I set the internal variables as 'global variables',
initialized both of them as '0', and terminated them as 'NULL'
by the end of the program, the simulation works as well as I
expect!!!!!

Anyway, I still do not know how to explain the phenomenon.
Why should I use global variables to fix out the problem, since
so many friends remind me of not using them.

Thank you very much for your kind help.

Best Regards!

Miaoxiao