Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Engine not sending the correct data

Subject: Engine not sending the correct data

From: Michael Stachowsky

Date: 07 Mar, 2008 17:23:02

Message: 1 of 2

I am trying to send an array of doubles from a C program
into a matlab function via the matlab engine.

No matter what I send it, the engine sets all my variables
to zero. as a test, I created an array of doubles ranging
from 1 to 1000, in equal steps of 1.

I then create the array as:

    //Create the Matlab variable that stors the times array
    timesArray = mxCreateDoubleMatrix(1,dataSize,mxREAL);
//The Array
    tmpPtr = mxGetPr(timesArray); //The Pointer
    
    //Copy dataset into myData
    memcpy((void *)tmpPtr, (void *)times, sizeof(times));

where times is my test array.

However, when I print out timesArray using:

engPutVariable(ep, "timesArrayMat", timesArray);
engEvalString(ep, "disp(num2str(timesArrayMat(50)))");
printf("Mandor\n %s... \n", buffer);

it shows me 0. I can do this for every value of timesArray
and I always get 0.

Any ideas?

Mike

Subject: Re: Engine not sending the correct data

From: James Tursa

Date: 07 Mar, 2008 21:51:03

Message: 2 of 2

"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;
}

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
engine James Tursa 07 Mar, 2008 16:55:04
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
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.
Related Topics