"Luca Cerone" <cerone@to-see.it> wrote in message
<g13rs5$64p$1@fred.mathworks.com>...
> 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-
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
Subject: Passing Arguments from compiled Matlab program to C program.
"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in
message <g14116$qf3$1@fred.mathworks.com>...
> "Luca Cerone" <cerone@to-see.it> wrote in message
> <g13rs5$64p$1@fred.mathworks.com>...
> > 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-
>
> 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
>
Subject: Passing Arguments from compiled Matlab program to C program.
> 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???
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:
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central.
Read the complete Disclaimer prior to use.