Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

using ssSetNumInputPorts but no change in inputports...

Asked by Echidna on 5 Dec 2012

I am trying to set the number of input ports of my s-function block (written in C-code). It seems to be easy but apparently I am overlooking something because after compiling my code, the s-function block still only has one input port (instead of the 7 defined). What am I missing? Below the code that I defined in mdlInitializeSizes

    int_T nInputPorts  = 7;  /* number of input ports  */
    int_T index;
    if (!ssSetNumInputPorts(S,nInputPorts)) return;
    if(!ssSetInputPortVectorDimension(S, 0, 6)) return;
    if(!ssSetInputPortVectorDimension(S, 1, 1)) return;
    if(!ssSetInputPortVectorDimension(S, 2, 2)) return;
    if(!ssSetInputPortVectorDimension(S, 3, 1)) return;
    if(!ssSetInputPortVectorDimension(S, 4, 1)) return;
    if(!ssSetInputPortVectorDimension(S, 5, 1)) return;
    if(!ssSetInputPortVectorDimension(S, 6, 1)) return;
    for (index = 0; index < 7; index++) {
        ssSetInputPortDirectFeedThrough(S, index, 1);
        ssSetInputPortComplexSignal(S, index, COMPLEX_NO);
    }
    ssSetNumSampleTimes(   S, 1);
    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);

1 Comment

Echidna on 5 Dec 2012

additionally, I can use ssGetInputPortRealSignalPtrs in mdlOutputs to access the different inputs so it seems that I have defined the number of inports correctly (otherwise it should not compile, no?) but no change in "triangles" in the s-function block to which I can connect the signals...

Echidna

Products

2 Answers

Answer by Babak on 5 Dec 2012
Edited by Babak on 5 Dec 2012

You need to command

 MEX thenameofyourCcode

In MATLAB's command window, and then reopen the model to see the change.

2 Comments

Echidna on 6 Dec 2012

of course I have first compiled my code (also given in the question above: after compiling my code, the s-function block still only...)

this is not what is missing

Babak on 6 Dec 2012

You need to put this line in the mdlInitializeSizes function as follows:

 static void mdlInitializeSizes(SimStruct *S)
 {
 if (!ssSetNumInputPorts(S, 7)) return;
 // ....
 }
Babak
Answer by Guy Rouleau on 6 Dec 2012

Your code looks good... I can't identify what is going wrong.

This is probably not the problem, but make sure MATLAB sees the correct s-function by executing "which -all nameOfYourFunction".

Also ensure mex really builds your last version by doing "clear mex" and deleting the existing mex-file.

If this is not the problem, try attaching a debugger and stepping line by line to better understand what is going on:

http://blogs.mathworks.com/seth/2012/12/06/debugging-cc-s-functions/

Note: Even if the ports do not exist, ssGetInputPortRealSignalPtrs will not give errors... it will just read wrong address in memory. You can enable the "Array Out of Bounds" diagnostic to see if this happens. Enable this diagnostic only for diagnostic, it is very expensive on performance.

3 Comments

Echidna on 7 Dec 2012

the which -all only gives me the correct file and other changes I make do result in the correct change in output so I am pretty sure the mex really builds my last version. For example, I have just put a Mux block before the s-function block and changed the width of the one input-port that does work to match the number of columns I have when all the inputsignals are merged together in one signal and this does seem to do the trick.

I find it strange that I need this extra block for my code to work fine, but I think I will have to use this solution so that I can continue my work for now (running into deadlines!)

the blog on debugging seems very useful, but I do not really understand how it would work in this case since the problem already arises before the simulation starts...

Echidna on 14 Dec 2012

okay, since some modifications are needed to my model, the Mux solution is not working for me anymore so I am trying to look into this problem again

When I download a debugger (as explained in the link provided), I step through the process and everything seems to work fine and after I finish debugging, the number of inports are changed for the s-function block in the file that I used for debugging. HOWEVER, when I open the library that contains the s-function block, the number of inports shown are again only one... Now I am completely lost...

Also if I recompile my code using 'mex nameCode', the number of inports is reset to zero instead of the changed number so it seems the debugger altered something in the mexCompilation file making it to work (but I have no idea what)

Guy Rouleau on 14 Dec 2012

Please try to simplify your code as much as possible and contact technical support. There is something strange here and without looking at your files this is very difficult to figure it out.

Guy Rouleau

Contact us