Azzera filtri
Azzera filtri

Iterate through custom bus datatype fields CMex

6 visualizzazioni (ultimi 30 giorni)
Rob
Rob il 10 Giu 2014
Hi,
I have some custom bus datatypes defined in my base MATLAB workspace. I now need to dynamically iterate through their fields to dynamically allocate enough memory, dynamically create structs with their structure and values, etc. pp.
My first intention was to search for a built-in function, but I couldnt find one. Then I saw the ssGetDataTypeName function, but I could not even get a value by calling mexCallMATLAB with ".Elements(1).DataType" appended to the result of the ssGetDataTypeName function. Instead an error gets thrown claiming MATLAb doesn't know a variable or function named like that.
Does anybody has a hint for me? Thanks in advance!
P.S.: Of course, all actions I try to perform from within the Simulink block succeed in the plain MATLAB environment doing it manually. There is no error like an undefined variable or a bad defined bus or something. It's working, just not dynamically.
  1 Commento
Rik van der Struijk
Rik van der Struijk il 6 Nov 2020
Modificato: Rik van der Struijk il 6 Nov 2020
Hi Rob,
I am trying to achieve the same as you. Have you ever found an elegant solution?

Accedi per commentare.

Risposte (1)

James Tursa
James Tursa il 10 Giu 2014
If you have a struct in your mex routine (call it st) and need to get at the .Elements(1).DataType field element, you will need to do it in steps.
mxArray *Elements, *DataType; // arbitrary variable names
Elements = mxGetField(st,0,"Elements"); // first get the Elements field
Datatype = mxGetField(Elements,0,"DataType"); // then get the DataType subfield
  1 Commento
Rob
Rob il 10 Giu 2014
Modificato: Rob il 10 Giu 2014
Thanks for your answer. I think you misunderstood, maybe I was unclear. I have bus datatypes (e.g. angular_bus). Those aren't structs, but kind of objects with the attribute Elements and each Elements element has an attribute DataType. So in the MATLAB console I could simply type (and I actually did to test it)
angular_bus.Elements(0).DataType
What I want to do is to get the Elements attribute and to determine the actual size of my input bus to allocate enough memory for it dynamically. The current implementation which was generated by the S-Function Builder looks something like this:
/* Bus Information */
slDataTypeAccess *dta = ssGetDataTypeAccess(S);
const char *bpath = ssGetPath(S);
DTypeId angular_busId = ssGetDataTypeId(S, "angular_bus");
DTypeId linear_busId = ssGetDataTypeId(S, "linear_bus");
DTypeId twist_busId = ssGetDataTypeId(S, "twist_bus");
int_T *busInfo = (int_T *)malloc(12*sizeof(int_T));
if(busInfo==NULL) {
ssSetErrorStatus(S, "Memory allocation failure");
return;
}
/* Calculate offsets of all primitive elements of the bus */
busInfo[0] = dtaGetDataTypeElementOffset(dta, bpath,twist_busId,0) +dtaGetDataTypeElementOffset(dta, bpath,linear_busId,0);
busInfo[1] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[2] = dtaGetDataTypeElementOffset(dta, bpath,twist_busId,0) +dtaGetDataTypeElementOffset(dta, bpath,linear_busId,1);
busInfo[3] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[4] = dtaGetDataTypeElementOffset(dta, bpath,twist_busId,0) +dtaGetDataTypeElementOffset(dta, bpath,linear_busId,2);
busInfo[5] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[6] = dtaGetDataTypeElementOffset(dta, bpath,twist_busId,1) +dtaGetDataTypeElementOffset(dta, bpath,angular_busId,0);
busInfo[7] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[8] = dtaGetDataTypeElementOffset(dta, bpath,twist_busId,1) +dtaGetDataTypeElementOffset(dta, bpath,angular_busId,1);
busInfo[9] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
busInfo[10] = dtaGetDataTypeElementOffset(dta, bpath,twist_busId,1) +dtaGetDataTypeElementOffset(dta, bpath,angular_busId,2);
busInfo[11] = dtaGetDataTypeSize(dta, bpath, ssGetDataTypeId(S, "double"));
ssSetUserData(S, busInfo);
I need to set those values dynamically by iterating through the bus (also nested buses), get their elements and get the DataType attribute of them, so I can calculate the total amount of needed memory. I only mentioned the struct to demonstrate that I cannot even access the bus datatypes defined in the MATLAB workspace by calling it directly (and as I think of it now, it didn't even make sense, because the DataType attribute wouldn't return a struct, so sorry).
Edit: To clarify, I attached a commented screenshot of the situation.

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by