Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Manipulation of mxArray struct into c++ code

Subject: Manipulation of mxArray struct into c++ code

From: Vialla

Date: 7 Jul, 2013 22:13:09

Message: 1 of 2

Hi everyone,

I am currently discovering the mex file programmation since I need to increase the compute speed of a matlab function (which uses a huge loop to solve a differential equation in time). This function needs many arguments (including three structures) and returns an array.

My questions :

1. Could you tell me what is the most efficient way : starting by converting my mxArray stuctures into standard c++ structures or should I only use mxArray object in my c++ file ?

2. If I should convert my structures : is there an easy way to do this ? I have something like 20 fields by structure, it seems to be a little bit onerous to assign each mxArray structure field to each c++ structure field manually...

I'm sorry for my weak skills in english. ..I swear I do my best.
Thank you !

Subject: Manipulation of mxArray struct into c++ code

From: Steven_Lord

Date: 8 Jul, 2013 14:59:14

Message: 2 of 2



"Vialla " <aikideus@hotmail.com> wrote in message
news:krcp5k$7vq$1@newscl01ah.mathworks.com...
> Hi everyone,
>
> I am currently discovering the mex file programmation since I need to
> increase the compute speed of a matlab function (which uses a huge loop to
> solve a differential equation in time). This function needs many arguments
> (including three structures) and returns an array.

Are you writing your own ODE solver instead of using one of the ODE solvers
provided by MATLAB?

http://www.mathworks.com/help/matlab/ordinary-differential-equations.html

> My questions :
>
> 1. Could you tell me what is the most efficient way : starting by
> converting my mxArray stuctures into standard c++ structures or should I
> only use mxArray object in my c++ file ?

I recommend against trying to convert (by which I assume you mean casting)
the mxArray to a struct array. If you really needed to you could extract the
appropriate fields of the mxArray that contains a MATLAB struct array using
the documented API and create a C++ struct using that data. But there is no
automated conversion method.

> 2. If I should convert my structures : is there an easy way to do this ? I
> have something like 20 fields by structure, it seems to be a little bit
> onerous to assign each mxArray structure field to each c++ structure field
> manually...

Use the functions that the C/C++ Matrix Library provides you for working
with the mxArrays. Don't try to "hack" it yourself; if you do, your hack may
break in future releases if we modify the internal details of the mxArray.
Using the supported and documented API we provide will insulate you if we do
need to make internal changes.

http://www.mathworks.com/help/matlab/cc-mx-matrix-library.html

The Access Data section lists many functions for manipulating mxArrays
containing MATLAB struct arrays.

> I'm sorry for my weak skills in english. ..I swear I do my best.

Your English was fine. The only words/phrases that seemed awkward were
"programmation" and "compute speed." I would use "programming" and
"computation speed" respectively instead.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us