Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Saving the parameters returned from mex

조회 수: 3 (최근 30일)
Jw
Jw 2012년 1월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
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;
}

답변 (1개)

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

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by