Info
This question is closed. Reopen it to edit or answer.
Saving the parameters returned from mex
1 view (last 30 days)
Show older comments
The timestamp from my mex file is passed back to mat file as data. I encountered that it saves the first and the last data it receives and the rest all zeros. How do i edit my mat file to ensure i get all the values? I guess the problem lies with the data=zeros(1,1) and the return parameter that is data. how do i change it?
Mat File:
clear;clc;
config;
data = zeros(1,1);
[status, validity, data] = readGPS(OPEN,'gps-2011-10-13-15-15.log');
index = 1;
while(status ~= STOP)
[status, validity, data] = readGPS(READ);
if validity == VALID_DATA
data(:,index) = data;
index = index + 1;
end
save('gpstime.mat','data')
end
Mex File:
double timestamp;
int status, validity;
FILE *fid;
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
int Op;
double *status_out, *validity_out, *data_out;
char data [17], buffer [256];
char *str;
/*check to find out the operation required*/
if (nrhs == 2)
{
Op = OPEN;
}
else
{
Op = READ;
}
/*Different cases for different operations*/
switch (Op)
{
case OPEN:
fid=fopen("gps-2011-10-13-15-15.log","r"); //open file as read
validity = INVALID;
timestamp = NO_VALUE;
if (fid != NULL)
{
status = CONTINUE;
}
else
{
status = STOP;
}
break;
case READ:
if(!feof(fid))
{
status = CONTINUE;
validity = VALID_DATA;
fgets(buffer, 255, fid); //read in the first line of the file
str = strtok(buffer, ","); //get the timestamp
strcpy(data, str);
timestamp = atof(data); //change from char to double
mexPrintf("Timestamp:%f \n",timestamp);
}
break;
case CLOSE:
fclose(fid);
validity = INVALID;
timestamp = NO_VALUE;
status = STOP;
break;
default:
mexErrMsgTxt("Incorrect parameter 3.");
break;
}
/*Passing back results*/
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); //create an mxArray
status_out = mxGetPr(plhs[0]); //get a pointer to the data
status_out[0] = status;
plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
validity_out = mxGetPr(plhs[1]);
validity_out[0] = validity;
plhs[2] = mxCreateDoubleMatrix(1,1,mxREAL);
data_out = mxGetPr(plhs[2]);
data_out[0] = timestamp;
}
0 Comments
Answers (1)
Friedrich
on 3 Jan 2012
Hi,
the problem is that you use the variable data for two different things. To store the overall data and as returnvalue of that mex file. Try it like this:
clear;clc;
config;
data = zeros(1,1);
[status, validity, data] = readGPS(OPEN,'gps-2011-10-13-15-15.log');
index = 1;
while(status ~= STOP)
[status, validity, tmp] = readGPS(READ);
if validity == VALID_DATA
data(:,index) = tmp;
index = index + 1;
end
end
save('gpstime.mat','data')
Is there a reason why you are doing this with a mex file? You can do this in MATLAB directly pretty easy.
5 Comments
Titus Edelhofer
on 4 Jan 2012
Just a comment: for the last lines of your mex file you might also use mxCreateDoubleScalar ...
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!