0

I am unable to convert my C file to mex file. Here is the C file I wrote to read in a tab-delimited file. The C program in running, but I am not able to successfully convert it to a mex file. I have also pasted the mex file I tried. Can anyone please tell me where is it that I am going wrong? I think I am making a mistake in declaring the structure. Also after declaring the output structure plhs[0], how do I retrieve it in MATLAB with desired structure elem?

C CODE:

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#define NAME "Cooley3_dynxfmr_20100501000000_20100930235930.txt"
#define INPUT 1
#define count 500000

int main(void)
{
//Structure
typedef struct _col{
    char date[20];
    float mva;
    char qc_load[1];
    float air;
    char qc_air[1];
    float oil;
    char qc_oil[1];
    float wind_a;
    char qc_wind_a[1];
    float wind_b;
    char qc_wind_b[1];
    float wind_c;
    char qc_wind_c[1];  
#if INPUT   
    float tamb1;
    char qc_tamb1[1];
#endif  

} col;

    char buf[1024];
char temp[20];
int count = 0;
int i, j, k, l;

//open file to count elements   
    int ptr_file =fopen(NAME,"r");
    if (ptr_file != NULL)
    {
    col *elem = malloc(count*sizeof(col));
//do stuff to read in the entire data into the structure elem.
    }
free(elem);
return 0;
}   

I attempted to convert it to mex as follows:

MEX FILE :

#include "mex.h"
#include "matrix.h"
#include <stdio.h>
#include<string.h>
#include<stdlib.h>  

void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{   
    char *NAME;
    NAME=mxArrayToString(prhs[0]);

    //Structure
    typedef struct _col{
    char date[20];
    float mva;
    char qc_load[1];
    float air;
    char qc_air[1];
    float oil;
    char qc_oil[1];
    float wind_a;
    char qc_wind_a[1];
    float wind_b;
    char qc_wind_b[1];
    float wind_c;
    char qc_wind_c[1];
    float tamb1;
    char qc_tamb1[1];
    } col;  

    char buf[1024];
char temp[20];
int count = 0;
int i, j, k, l;

//open file to count elements   
    int ptr_file =fopen(NAME,"r");
    if (ptr_file != NULL)
    {
    const char *field_names[] = {"date", "mva", "qc_load", "air", "qc_air", "oil", "qc_oil", "wind_a", "qc_wind_a", "wind_b", "qc_wind_b","wind_c","qcwind_c","tamb1","qc_tamb1"};  
    mwSize dims[2] = {1, count };
    plhs[0] = mxCreateStructArray(2, dims, 15, field_names);
    col *elem 
    elem = mxCalloc(count,sizeof(col));

//do stuff to read in the entire data into the structure elem.
    }
plhs[0] = (mxArray*)col;
mxFree(elem);
}

Edited by srao1

1
Contributor
1
Reply
11
Views
3 Years
Discussion Span
Last Post by srao1
0

this is how I tried to copy the data to the output structure. However I am still not able to convert it to a mex file.

 for (i=0; i<count; i++) 
    {
        mxArray *field_value;
        mxSetFieldByNumber(plhs[0],i,date,mxCreateString(elem[i].date));

        field_value = mxCreateDoubleMatrix(1,1,mxREAL);
        *mxGetPr(field_value) = elem[i].mva;
         mxSetFieldByNumber(plhs[0],i,mva,field_value);                          mxSetFieldByNumber(plhs[0],i,qc_load,mxCreateString(elem[i].qc_load));

// similarly for other fields of the structure.

        }

Edited by srao1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.