Passing Arguments from compiled Matlab program to C program.

Passing Arguments from compiled Matlab program to C program.

Post by Luca Ceron » Fri, 23 May 2008 22:19:02


Hi,
compiled one of my Matlab Programs let's call it MatProg.
It takes in input a string and it gives a string as an output.

I want to use the output of my compiled Matlab code as an
input for one function written in C.

I could do this making Matlab display the output string on
the standar output device and making the C program to read it.

I don't like this solution as there is a limit of 25000
characters.

Is there a way to use a matlab function as a c function?

I mean in C i want to write code like this:

outstring=MatProg(inputString);

a=myCfunction(outstring);

Is this possible???

Thank you very much in advance!
See you -Luca-
 
 
 

Passing Arguments from compiled Matlab program to C program.

Post by James Turs » Fri, 23 May 2008 23:47:02


<g13rs5$64p$ XXXX@XXXXX.COM >...

You don't specify exactly how you compiled the MATLAB code,
but here are a couple of suggestions that may work for you:

If your C program is compiled as an engine application, you
could do the following:

- convert the C string to a MATLAB mxArray string using
mxCreateString
- put the result into the engine workspace using engPutVariable
- call your MatProg function from the engine using engEvalString
- get the result into your C program using engGetVariable
- convert the result to C string using mxArrayToString
- call myCfunction with the result

If your C program is a mex function it is a bit simpler:

- convert the C string to a MATLAB mxArray string using
mxCreateString
- call your MatProg function using mexCallMATLAB
- convert the result to C string using mxArrayToString
- call myCfunction with the result

The problem with these solutions, however, is that the
strings in question are doubled up in memory (tripled up in
engine applications). MATLAB mxArray variables store them as
an array of 16-bit elements with no null terminator, whereas
C needs them as an array of 8-bit characters with a null
terminator. You could get rid of one of the copies by
rewriting your myCfunction so that it expects the MATLAB
style strings instead of C style strings. Then you could use
mxGetData and just pass the pointer to your myCfunction
without making a copy.

James Tursa

 
 
 

Passing Arguments from compiled Matlab program to C program.

Post by Luca Ceron » Sat, 24 May 2008 16:53:02

I couldn' understand a word :)



"James Tursa" < XXXX@XXXXX.COM > wrote in
message <g14116$qf3$ XXXX@XXXXX.COM >...

output.
read it.
engPutVariable
engEvalString
 
 
 

Passing Arguments from compiled Matlab program to C program.

Post by Peter Boet » Sat, 24 May 2008 22:14:27

"Luca Cerone" < XXXX@XXXXX.COM > writes:


As James alluded to, there are two efficient ways to hook up C and
MATLAB programs.

MEX files allow you to compile C routines and call them from MATLAB.
The C routine has direct access to MATLAB variables passed as parametes.

MATLAB "Engine" programs allow you to open a MATLAB session from C and
execute MATLAB commands.

See the External Interfaces section of the documentation, starting from here:

http://www.yqcomputer.com/


-Peter