Products & Services Solutions Academia Support User Community Company

eml.cstructname - Package: eml

Specify structure name in generated code

Syntax

eml.cstructname(structVar, 'structName')
eml.cstructname(structVar, 'structName', 'extern')

Description

eml.cstructname(structVar, 'structName') allows you to specify the name of a structure in generated code. structVar is the structure variable. structName specifies the name to use for the structure.

eml.cstructname(structVar, 'structName', 'extern') declares an externally defined structure. The Embedded MATLAB subset does not generate the definition of the structure type; provide it in a custom include file.

You must call eml.cstructname before the first use of the structure variable in your function.

eml.cstructname has no effect in MATLAB code; it applies to the Embedded MATLAB subset only. Using eml.cstructname at the MATLAB command line and then calling emlc does not assign a name to a structure in the generated code.

Examples

Apply eml.cstructname to top-level inputs:

  1. Write an Embedded MATLAB compliant M-function topfun that assigns the name MyStruct to its input parameter.

    function y = topfun(x)   %#eml
    % Instruct Embedded MATLAB to assign the name 'MyStruct' 
    % to the input variable
    eml.cstructname(x, 'MyStruct');
    y = x;
    
  2. Declare a structure s in MATLAB. s is the structure definition for the input variable x.

    s = struct('a',42,'b',4711);
  3. Generate a MEX function for topfun, using the -eg option to specify that the input parameter is a structure.

    emlc -eg { s } topfun.m

    emlc generates a MEX function in the default folder emcprj\mexfcn\topfun. The structure definition is in topfun_types.h in this folder.

    typedef struct
    {
        real_T a;
        real_T b;
    } MyStruct;
 

Assign the name MyStruct to the structure structVar and pass the structure to a C function use_struct:

  1. Create a C header file use_struct.h for a function use_struct that takes a parameter of type MyStruct. Define a structure of type MyStruct in the header file.

    #include <tmwtypes.h>
    
    typedef struct MyStruct
    {
        real_T s1;
        real_T s2;
    } MyStruct;
    
    void use_struct(struct MyStruct *my_struct);
    
  2. Write the C function use_struct.c.

    #include <stdio.h>
    #include <stdlib.h>
    
    #include "use_struct.h"
    
    void use_struct(struct MyStruct *my_struct)
    {
      real_T x = my_struct->s1;
      real_T y = my_struct->s2;
    }
    
  3. Write an Embedded MATLAB compliant M-function m_use_struct that declares a structure, assigns the name MyStruct to it, and then calls the C function use_struct using eml.ceval.

    function m_use_struct   %#eml
    % The directive %#eml declares the function
    % to be Embedded MATLAB compliant
    % Declare a MATLAB structure
    structVar.s1 = 1;
    structVar.s2 = 2;
    
    % Instruct Embedded MATLAB to assign the name MyStruct
    % to the structure variable. extern indicates this is
    % an externally defined structure.
    eml.cstructname(structVar, 'MyStruct', 'extern');
    
    % Call the C function use_struct. The type of structVar 
    % matches the signature of use_struct.
    % Use eml.rref to pass the the variable structVar by 
    % reference as a read-only input to the external C 
    % function use_struct
    eml.ceval('use_struct', eml.rref(structVar));
    
  4. Generate C library code for M-function m_use_struct, passing use_struct.h to include the structure definition.

    emlc -T rtw:lib m_use_struct use_struct.c use_struct.h

    emlc generates C code in the default folder emcprj\rtwlib\m_use_struct. The generated header file m_use_struct_types.h in this folder contains no definition of the structure MyStruct because MyStruct is an external type.

See Also

eml.ceval | eml.rref | emlc

How To

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS