简体   繁体   English

从mex访问Matlab Struct

[英]Access Matlab Struct from mex

I have a Structure from Matlab passed to mex. 我有一个Matlab的结构传递给了mex。 It is passed correctly, I verified it with mxGetClassName(mxArray_pointer_carrying_struct) which returns struct as the class type. 它正确传递,我用mxGetClassName(mxArray_pointer_carrying_struct)进行了验证,该函数返回struct作为类类型。 The struct has 15 fields n corresponding properties, all 30 Strings (15 property_names, 15 property_values). 该结构具有15个字段和相应的属性,全部为30个字符串(15个property_names,15个property_values)。

I am able to access property names using mxGetFieldNameByNumber(mxArray_pointer_carrying_struct, index); 我可以使用mxGetFieldNameByNumber(mxArray_pointer_carrying_struct,index)访问属性名称;

How can I access the property values? 如何访问属性值?

The code I have to do above looks as below: 我上面要做的代码如下:

extract_settings(const mxArray *p)
{
    mwIndex j = 1;
    const char *property;
    mexPrintf("\nInput Arg %i is of type:%s\n",j,mxGetClassName(p));
    for(int i = 0;i<=14;i++)
    {
        property = mxGetFieldNameByNumber(p, i);  %gets property names
        mexPrintf("%s-- \n",property); %Displays 15 property names
    }
}

My struct Looks as below : 我的结构看起来如下:

{ 
TRIGGER_POLARITY : LEVEL_LOW
EDGE : EDGE_RISING 
. 
.
. (15 elements as of now)
}

You're probably looking for mxGetFieldByNumber . 您可能正在寻找mxGetFieldByNumber There's also a full example for passing structs to MEX files shipped with MATLAB, see this documentation from Mathworks . 还有一个完整的示例,可将结构传递给MATLAB随附的MEX文件,请参阅Mathworks的本文档 You can load the example in MATLAB as follows: 您可以按以下方式在MATLAB中加载示例:

edit([matlabroot '/extern/examples/refbook/phonebook.c']);

EDIT: There's also mxGetField which lets you access the field using its name. 编辑:还有mxGetField ,您可以使用它的名称访问该字段。

EDIT2: To convert the result from mxGetField to a C string you can use mxArrayToString . EDIT2:要将结果从mxGetField转换为C字符串,可以使用mxArrayToString Note that you need to free the string's memory after you have used it. 请注意,使用完字符串后,需要释放该字符串的内存。 You can use mxIsChar to check whether the field contains a MATLAB character array. 您可以使用mxIsChar来检查该字段是否包含MATLAB字符数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM