Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

problem writing s-function (c/c++) inputports problem with arrays

problem writing s-function (c/c++) inputports problem with arrays

problem writing s-function (c/c++) inputports problem with arrays

I am a new user of simulink and I'm sure my problem is quite simple for someone with some experience writing s-functions. I am attempting to write an s-function in c++ that takes as input 3 double[][3] arrays(the row value is to be dynamicaly_size) and 3 integer values as input. The output I want is another double array of size 4x4. In mdloutputs all that I want to happen is a call to my c++ registration function code using the input values and the ouput to be the updated version of the 4x4 matrix that is adjusted in the registration call. The parmeters for egistration are:

Registration(double [][3], int32, double [][3], int32, double [][3], int32, double[4][4])

I do not entirely understand the use of Real_T for casting. My registration function wont recognize real_T values (I thought perhaps being cast as Real_T would automatically read the input as its true type as I declared when initializing). So my question is, how would I cast my input to make registration recognize the inputs as double[][3] and the output as a ?

Here is my code(All you probably need to look at is mdlInitializeSizes and mdlOutputs):
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
#include "simstruc.h"
#include "registration.h"

/* Error handling
* --------------
* You should use the following technique to report errors
encountered within
* an S-function:
* ssSetErrorStatus(S,"Error encountered due to ...");
* return;
* Note that the 2nd argument to ssSetErrorStatus must be persistent
* It cannot be a local variable. For example the following will
* unpredictable errors:
* mdlOutputs()
* {
* char msg[256]; {ILLEGAL: to fix use "static char
* sprintf(msg,"Error due to %s", string);
* ssSetErrorStatus(S,msg);
* return;
* }
* See matlabroot/simulink/src/sfuntmpl_doc.c for more details.

* S-function methods *

/* Function: mdlInitializeSizes
* Abstract:
* The sizes information is used by Simulink to determine the
* block's characteristics (number of inputs, outputs, states,
static void mdlInitializeSizes(SimStruct *S)
/* See sfuntmpl_doc.c for more details on the macros below */

ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual
parameters */

ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);

if (!ssSetNumInputPorts(S, 6)) return;
ssSetInputPortMatrixDimensions(S, 0, DYNAMICALLY_SIZED, 3);
ssSetInputPortDataType(S, 1, SS_INT32);
ssSetInputPortWidth(S, 1, 1);
ssSetInputPortMatrixDimensions(S, 2, DYNAMICALLY_SIZED, 3);
ssSetInputPortDataType(S, 3, SS_INT32);
ssSetInputPortWidth(S, 3, 1);
ssSetInputPortMatrixDimensions(S, 4, DYNAMICALLY_SIZED, 3);
ssSetInputPortDataType(S, 5, SS_INT32);
ssSetInputPortWidth(S, 5, 1);
ssSetInputPortMatrixDimensions(S, 6, 4, 4);

ssSetInputPortRequiredContiguous(S, 0, true); /*direct input
signal access*/
ssSetInputPortRequiredContiguous(S, 1, true);
ssSetInputPortRequiredContiguous(S, 2, true);
ssSetInputPortRequiredContiguous(S, 3, true);
ssSetInputPortRequiredContiguous(S, 4, true);
ssSetInputPortRequiredContiguous(S, 5, true);
ssSetInputPortRequiredContiguous(S, 6, true);

* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
//i think this should be zero for all since they do not directly c
ssSetInputPortDirectFeedThrough(S, 0, 0);
ssSetInputPortDirectFeedThrough(S, 1, 0);
ssSetInputPortDirectFeedThrough(S, 2, 0);
ssSetInputPortDirectFeedThrough(S, 3, 0);
ssSetInputPortDirectFeedThrough(S, 4, 0);
ssSetInputPortDirectFeedThrough(S, 5, 0);

if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortMatrixDimensions(S, 0, 4, 4);

ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);

ssSetOptions(S, 0);

static void mdlInitializeSampleTimes(SimStruct *S)
//what kind of sample time should be used
//ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);


#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove
function */

static void mdlInitializeConditions(SimStruct *S)

#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)

static void mdlStart(SimStruct *S)
#endif /* MDL_START */

static void mdlOutputs(SimStruct *S, int_T tid)
real_T *in1 = (real_T*) ssGetInputPortSignal(S,0);
real_T *in2 = (real_T*) ssGetInputPortSignal(S,1);
real_T *in3 = (real_T*) ssGetInputPortSignal(S,2);
real_T *in4 = (real_T*) ssGetInputPortSignal(S,3);
real_T *in5 = (real_T*) ssGetInputPortSignal(S,4);
real_T *in6 = (real_T*) ssGetInputPortSignal(S,5);

double matrix[4][4];


real_T *y = ssGetOutputPortSignal(S,0); //this is fine
y[0] = matrix;

#define MDL_UPDATE /* Change to #undef to remove function */
#if defined(MDL_UPDATE)

static void mdlUpdate(SimStruct *S, int_T tid)
#endif /* MDL_UPDATE */

#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)

static void mdlDerivatives(SimStruct *S)
#endif /* MDL_DERIVATIVES */

static void mdlTerminate(SimStruct *S)

#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a
MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#include "cg_sfun.h" /* Code generation registration function

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close