Path: news.mathworks.com!not-for-mail
From: "James Tursa" <aclassyguywithaknotac@hotmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Engine not sending the correct data
Date: Fri, 7 Mar 2008 21:51:03 +0000 (UTC)
Organization: Boeing
Lines: 70
Message-ID: <fqsdc7$pd7$1@fred.mathworks.com>
References: <fqrtlm$qfb$1@fred.mathworks.com>
Reply-To: "James Tursa" <aclassyguywithaknotac@hotmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1204926663 26023 172.30.248.37 (7 Mar 2008 21:51:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 7 Mar 2008 21:51:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:456043


"Michael Stachowsky" <mstachowsky@gmail.com> wrote in 
message <fqrtlm$qfb$1@fred.mathworks.com>...
> I am trying to send an array of doubles from a C program
> into a matlab function via the matlab engine.  
> 
> Any ideas?
> 
> Mike

I have taken your example statements and constructed a 
complete engine example that does what I think you want. 
It prints a non-zero value. I left the engine running 
after the program ends. Hopefully you can use this to help 
debug your code.

Side comment: I think you are doing too much work with 
your engEvalString call. You don't need to "disp(num2str
(timesArrayMat(50)))". Since all output that MATLAB would 
normally send to the console gets put into the buffer, you 
can simply do this: "timesArrayMat(50)". The whole 
business of converting the number to a string and 
displaying it will happen automatically, since that is 
what would normally happen at the MATLAB prompt for this 
statement.

James Tursa

--------------------------------------------

#include <stdio.h>
#include <string.h>
#include "engine.h"

#define dataSize 1000
#define bufferlength 2000

int main()
{
    char buffer[bufferlength];
    double times[dataSize];
    mxArray *timesArray;
    double *tmpPtr;
    Engine *ep;
    int i;

    if( (ep = engOpen(NULL)) == NULL )
        return 1;  // error
    engOutputBuffer( ep, buffer, bufferlength );

    for( i=0; i<dataSize; i++ )
        times[i] = (double) (i+1);

    timesArray = mxCreateDoubleMatrix( 1, dataSize, 
mxREAL );
    if( timesArray == NULL )
        return 1;  // error
    tmpPtr = mxGetPr( timesArray );
    memcpy( (void *)tmpPtr, (void *)times, sizeof(times) );

    if( engPutVariable( ep, "timesArrayMat", timesArray ) )
        return 1;  // error
    engEvalString( ep, "disp(num2str(timesArrayMat
(50)))" );
    printf("mandor\n %s... \n", buffer );

    mxDestroyArray( timesArray );

    return 0;
}