Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Saving the parameters returned from mex

1 visualización (últimos 30 días)
Jw
Jw el 3 de En. de 2012
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
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;
}

Respuestas (1)

Friedrich
Friedrich el 3 de En. de 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 comentarios
Friedrich
Friedrich el 4 de En. de 2012
Could you please mark it as accepted than? Thanks.
Titus Edelhofer
Titus Edelhofer el 4 de En. de 2012
Just a comment: for the last lines of your mex file you might also use mxCreateDoubleScalar ...

La pregunta está cerrada.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by