1

i need an idea how to make an s-function behave as a bus selector. i have an structure being given as a input to an s-function . this structure has 283 elements (maybe more in future) and is being supplied as a bus. i want the s-function to output the individual elements of the structure (and thus act like a bus selector). Ofcourse I can do this easily with manual typing :

y0[0]=u0->arguemtn;
y0[0]=u0->speedx; % and so on till 283

but then i need to know the name of the all the structure element and also need to enter them manually. is there a way i just need to use a simple for loop and allocate individual element inputs to outputs of the s-function.

you can just give me the hint since i am just stuck here

UPDATE

after the suggested answer i tried to write something like this.

int number_of_elements,i;
char field_name;
number_of_elements= mxGetNumberOfFields(u0[0]);

for(i=0;i<number_of_elements;i++)
{
    field_name=mxGetFieldNameByNumber(u0[0], i);
    yi[0]=u0->field_name;
}

but first how can i make y1 y2 y3 and so on as a part of the loop. ofcourse i can't write yi since its an entirely different variable. also this part I have written in the Code Description part of the s-function builder which I think is wrong again. can anybody suggest me what i should do exactly.

another important point is that I am sending the structure with fields which are of different datatypes (like uint8,uint16,single) and thus i also need to determine the input datatype. how can that be possible and how i can set this datatype to my output in a loop?

Arun Kumar
  • 103
  • 13

1 Answers1

1

It looks like you're using a C-code S-Function, in which case all of the C-mex API routines are available.

There are a variety functions for interrogating the Structure, determining the field names, and manipulating them. See here for a link to them, including mxGetField and mxGetNumberOfFields. Start by looking at and working through the example discussed here.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • i have a doubt regarding this. since i won't know the number of fields in my structure and thus I won't know the number of outputs in my s-function builder block, how can i make this dynamic? – Arun Kumar Mar 05 '15 at 14:54
  • In the S-Function initialization you use the appropriate API function to interrogate the structure (input as a parameter) to determine the number of fields, then you use other appropriate API functions to set the number of outputs (and other properties). – Phil Goddard Mar 05 '15 at 15:00
  • hey i updated my question. while i was going through the s-function documentation i found out this. that the s-function's limitation is that it doesn't support bus signals http://de.mathworks.com/help/simulink/sfg/s-function-limitations.html so any idea how to approach this problem? – Arun Kumar Mar 06 '15 at 07:45
  • With what you are doing here, you should be using a hand written c-mex S-function, which do support busses. – Phil Goddard Mar 06 '15 at 15:07