matlab mxarray array,[Matlab]MxArray与MwArray使用区别

本文介绍了在与MATLAB交互时,两种主要的Array接口——C接口的MxArray和C++接口的MwArray。MxArray需要手动管理内存,而MwArray具有简洁的声明和自动销毁功能。文章详细阐述了两者在声明、销毁、变量传递等方面的区别,并提供了相关函数的使用示例,如创建、获取数组信息和数据类型验证等。
摘要由CSDN通过智能技术生成

引子

在外部编程语言与matlab的交互中,Array是最单元的交互元素,怎么都绕不过去。

在matlab提供的Array接口有两个,一个是C的MxArray, 另一个是Cpp(C++)的MwArray.

看下两着的分别介绍:

mxArray:Matlab C 函数库的结构体

mwArray:Matlab C++ 函数库中对mxArray的包装类

声明:

mxArray:mxArray *a;

mwArray:mwArray a;

销毁

mxArray:mxDestroyArray a;

mwArray:mwArray类的析构函数自动销毁对象

变量传递

mxArray:mxArray *dest_ptr =mxCreateDoubleMatrix(rows,cols, mxREAL);

memcpy(dest_ptr,source_ptr,MAX_SIZE);

mwArray:mwArray in1(rows, cols, mxDOUBLE_CLASS, mxREAL);

mwArray in2(rows, cols, mxDOUBLE_CLASS, mxREAL);

in1.SetData(data, rows*cols);

in2.SetData(data, rows*cols);

比较而言, 1。mwArray的声明更简洁,不用考虑指针 2。mwArray不用手动释放内存

mxArray 介绍

mxArray *mxCreateDoubleMatrix(int m, int n, mxComplexity ComplexFlag);

参数m和n为矩阵的函数和列数。ComplexFlag为常数,用来区分矩阵中元素是实数还是复数,取值分别为mxREAL和mxCOMPLEX。

类似的创建函数还有:

mxArray *mxCreateString(const char *str); 创建一个字符串类型并初始化为str字符串。

对应的,要删除一个数组mxDestroyArray,该函数声明如下:

void mxDestroyArray(mxArray *array_ptr);

要获得mxArray数组每一维上元素的个数,可以用mxGetM和mxGetN函数。其中mxGetM用来获得数组第一维的元素个数,对于矩阵来说就是行数。

int mxGetM(const mxArray *array_ptr); //返回array_ptr对应数组第一维的元素个数(行数)

int mxGetN(const mxArray *array_ptr); //返回array_ptr对应数组其它维的元素个数,对于矩阵来说是列数。对于多维数组来说是从第2维到最后一维的各维元素个数的乘积。

要获得某一特定维的元素个数,则要用函数:

const int *mxGetDimensions(const mxArray *array_ptr);

该函数返回array_ptr各维的元素个数保存在一个int数组中返回。对于常用的矩阵来说,用mxGetM和mxGetN两个函数就可以了。

另外还可以通过mxGetNumberOfDimensions来获得数组的总的维数,用mxSetM、mxSetN设置矩阵的行数和列数,函数说明如下:

int mxGetNumberOfDimensions(const mxArray *array_ptr); //返回数组的维数

void mxSetM(mxArray *array_ptr, int m); //设置数组为m行

void mxSetN(mxArray *array_ptr, int n); //设置数组为n列

在对mxArray类型的变量进行操作之前,可以验证以下其中的数组的数据类型,比如是否为double数组、整数、字符串、逻辑值等,以及是否为某种结构、类、或者是特殊类型,比如是否为空数组,是否为inf、NaN等。常见的判断函数有:

Use these functions to validate input arguments.

C Functions

mxIsDouble

Determine whether mxArray represents data as double-precision, floating-point numbers

mxIsSingle

Determine whether array represents data as single-precision, floating-point numbers

mxIsComplex

Determine whether data is complex

mxIsNumeric

Determine whether array is numeric

mxIsInt64

Determine whether array represents data as signed 64-bit integers

mxIsUint64

Determine whether array represents data as unsigned 64-bit integers

mxIsInt32

Determine whether array represents data as signed 32-bit integers

mxIsUint32

Determine whether array represents data as unsigned 32-bit integers

mxIsInt16

Determine whether array represents data as signed 16-bit integers

mxIsUint16

Determine whether array represents data as unsigned 16-bit integers

mxIsInt8

Determine whether array represents data as signed 8-bit integers

mxIsUint8

Determine whether array represents data as unsigned 8-bit integers

mxIsScalar

Determine whether array is scalar array

mxIsChar

Determine whether input is mxChar array

mxIsLogical

Determine whether array is of type mxLogical

mxIsLogicalScalar

Determine whether scalar array is of type mxLogical

mxIsLogicalScalarTrue

Determine whether scalar array of type mxLogical is true

mxIsStruct

Determine whether input is structure array

mxIsCell

Determine whether input is cell array

mxIsClass

Determine whether array is member of specified class

mxIsInf

Determine whether input is infinite

mxIsFinite

Determine whether input is finite

mxIsNaN

Determine whether input is NaN (Not-a-Number)

mxIsEmpty

Determine whether array is empty

mxIsSparse

Determine whether input is sparse array

mxIsFromGlobalWS

Determine whether array was copied from MATLAB global workspace

mxAssert

Check assertion value for debugging purposes

mxAssertS

Check assertion value without printing assertion text

对于常用的double类型的数组,可以用mxGetPr和mxGetPi两个函数分别获得其实部和虚部的数据指针,这两个函数的声明如下:

double *mxGetPr(const mxArray *array_ptr); //返回数组array_ptr的实部指针

double *mxGetPi(const mxArray *array_ptr); //返回数组array_ptr的虚部指针

Utilities for manipulating strings and structures.

C Functions

mxArrayToString

Array to string

mxArrayToUTF8String

Array to string in UTF-8 encoding

mxGetString

mxChar array to C-style string or Fortran character array

另外一种操作mxArray的方法(在mathworks上居然没有搜索到..... 囧)

//为了调用matlab中的函数,必须使用数组数据类型,并其后调用matlab函数将其转化为矩阵格式(matlab的基本数据类型是矩阵)

static double x1[1]={1.0};

static double x2[1]={2.5};

double result;

//调用matlab创建3个矩阵

mxArray *A=mclGetUninitializedArray();

mxArray *B=mclGetUninitializedArray();

mxArray *C=mclGetUninitializedArray();

//将C语言中的变量值赋给matlab中的矩阵

mlfAssign(&A,mlfDoubleMatrix(1,1,x1,NULL));

mlfAssign(&B,mlfDoubleMatrix(1,1,x2,NULL));

mlfAssign(&C,mlfMyfunct(A,B)); //调m函数

//将matlab中的矩阵的指针传递给C语言中的指向double的指针

double * md=mxGetPr(C);

result=md[0];

//释放这些矩阵

mxDestroyArray(A);

mxDestroyArray(B);

mxDestroyArray(C);

C++ Utility Classes

mwArray

Class used to pass input/output arguments to C functions generated by MATLAB Compiler SDK

mwException

Exception type used by the mwArray API and the C++ interface functions

mwString

String class used by the mwArray API to pass string data as output from certain methods

mwArray 介绍

构造函数Constructors

mwArray() Description 创建空的Matlab阵列,类型为mxDOUBLE_CLASS

mwArray(mxClassID mxID) Description 创建mxID指定类型的Matlab阵列

Arguments

mxClassID mxID

Valid mxClassID specifying the type of array to construct. See the Work with mxArrays for more information on mxClassID.

mwArray(mwSize num_rows, mwSize num_cols, mxClassID mxID, mxComplexity cmplx = mxREAL)

Description 创建行数为num_rows,列数为num_cols,类型为mxID的Matalb阵列,对于数值型阵列,将complx做为最后一个参数,确定待创建阵列是否为复数阵列

Arguments

mwSize num_rows

Number of rows in the array

mwSize num_cols

Number of columns in the array

mxClassID mxID

Valid mxClassID specifying the type of array to construct. See the Work with mxArrays for more information on mxClassID.

mxComplexity cmplx

Complexity of the array to create. Valid values are mxREAL and mxCOMPLEX. The default value is mxREAL.

mwArray(mwSize num_dims, const mwSize* dims, mxClassID mxID, mxComplexity cmplx = mxREAL)

Description

创建任意维数的Matlab阵列,维数由num_dims指定,各维大小由dims指定,mxID指定阵列的类型。对于数值型阵列,将cmplx作为最后的一个参数,确定待创建阵列是否为复型的阵列。

All elements are initialized to zero. For cell arrays, all elements are initialized to empty cells.

Arguments

mwSize num_dims

Number of dimensions in the array

const mwSize* dims

Dimensions of the array

mxClassID mxID

Valid mxClassID specifying the type of array to construct. See the Work with mxArrays for more information on mxClassID.

mxComplexity cmplx

Complexity of the array to create. Valid values are mxREAL and mxCOMPLEX. The default value is mxREAL.

mwArray(const char* str)

Description

Create a 1-by-n array of type mxCHAR_CLASS, with n = strlen(str), and initialize the array's data with the characters in the supplied string.

根据字符串str创建一个新的字符型阵列

Arguments

const char* str

Null-terminated character buffer used to initialize the array

mwArray(mwSize num_strings, const char** str)

Description

创建字符型阵列(mxCHAR_CLASS),字符串由str指定. The created array has dimensions m-by-max, where max is the length of the longest string in str.

Arguments

mwSize num_strings

Number of strings in the input array

const char** str

Array of null-terminated strings

mwArray(mwSize num_rows, mwSize num_cols, int num_fields, const char** fieldnames)

Description

Create a matrix of type mxSTRUCT_CLASS, with the specified field names. All elements are initialized with empty cells.

创建行数为num_rows,列数为num_cols结构体阵列(mxSTRUCT_CLASS), 结构体域名为由fieldnames指定,域名个数由num_fields指定

Arguments

mwSize num_rows

Number of rows in the array

mwSize num_cols

Number of columns in the array

int num_fields

Number of fields in the stru

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值