×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

MatLab and C Interface

MatLab and C Interface

MatLab and C Interface

(OP)
Hey All,
I'm writting code to interface a matlab script with an algorithm based in C, and it seems to have a problem pasing data (in my case a 1D array) into the MEX function.  Whenever i pass in the array to the MEX function the code knows that its an nth-element, 1D array but all the data values within the array get zero'ed out for whatever reason.

To try and debug the issue i removed my C algo from the picture and delt purely with the MatLab-to-C interface.  Below is the code, without my algo, that exhibits this issue.

-----------------code--------------------
#include "mex.h"
#include "matrix.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    double *xData;
    int i, rows;
    
    rows = mxGetN(prhs[0]);
    xData = mxGetPr(prhs[0]);
    
    for(i = 0; i < rows; i++){
        printf("xData[%i] = %d\n", i, xData[i]);
    }
       
    return;
}
----------------------------------------------

its a simple function that takes the array that gets passed into the MEX function and prints it to the matlab console.

To replicate this issue i would compile this .c file with the C compiler that comes with the MatLab R2007a rev. and would call this fucntion from the matlab command line with the follwoing command

>> testMex([1,2,3,4,5,6])

and it would output

xData[0] = 0
xData[1] = 0
xData[2] = 0
xData[3] = 0
xData[4] = 0
xData[5] = 0
>>

does anybody have any idea why the data values dont get transfered but the memory space/size does?

thx

RE: MatLab and C Interface

(OP)
I'm not sure what you mean by "original array" which array are you referring to?

thx

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources