Mex

2 次查看(过去 30 天)
Jw
Jw 2011-12-29
Apologise for my previous post as I posted the wrong coding. I have this error One or more output arguments not assigned during call to "readData". pls assist to let me know where to correct the error. Thanks all.
Error in ==> main at 6 [status, validity, data] = readData(OPEN,'gps-2011-10-13-15-15.log');
clear;
config;
data = zeros(1,1);
[status, validity, data] = readData(OPEN,'gps-2011-10-13-15-15.log');
index = 1;
while(status ~= EOF)
[status, validity, data] = readData(READ);
if validity == VALID_DATA
data(:,index) = data;
index = index + 1;
end
end
===================================
Mex file
===================================
#define OPEN 0
#define CLOSE 1
#define READ 2
#define VALID_DATA 1
#define INVALID -1
#include "mex.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <matrix.h>
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 = -1;
if (fid != NULL)
{
status = 1;
}
else
{
status = -1;
}
return;
case READ:
while(!feof(fid))
{
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);
}
return;
case CLOSE:
fclose(fid);
timestamp = -1;
validity = INVALID;
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[1] = validity;
plhs[2] = mxCreateDoubleMatrix(1,1,mxREAL);
data_out = mxGetPr(plhs[2]);
data_out[2] = timestamp;
mxFree(status_out);
mxFree(validity_out);
mxFree(data_out);
}

采纳的回答

Friedrich
Friedrich 2011-12-29
Hi,
I think the issue is raised in your switch statement when you open a file. You have a return in there, which leads to terminating the mex file without assigning the output variables. Change the return to a break and it should work.
In addition these line should generate a SegV:
plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL);
validity_out = mxGetPr(plhs[1]);
validity_out[1] = validity;
Since validity_out[1] will access the second field in an array with one field only. This should be
validity_out[0] = validity;
The same applies on timestamp.
You dont need to apply mxFree on the xx_out variables. Since the weren't allocated woth mxMalloc and in addition they get deleted automatically when the mex function is finshed running.
  2 个评论
Jw
Jw 2011-12-29
thank you
Jw
Jw 2011-12-30
I have multiple timestamps and when i return to my matlab, it only stores the 1st. I think the problem lies with the data = zeros(1,1) and the return of my [status, validity, data]. how do i create a temp storage for the data coming in so that i can get all my data into matlab?

请先登录,再进行评论。

更多回答(2 个)

Titus Edelhofer
Titus Edelhofer 2011-12-29
Hi,
the return statements in the switch-case block are suspicious: you are leaving the mex file without entering the "passing back results" section. I guess you need to replace them by "break" ...?
Titus
  1 个评论
Titus Edelhofer
Titus Edelhofer 2011-12-29
BTW: why do you write this as a MEX file? Using more or less the same MATLAB code should be easier ...

请先登录,再进行评论。


James Tursa
James Tursa 2011-12-29
The "return" statement error was pointed out several days ago in OP's previous thread, it just wasn't corrected. In addition, the mxFree statements at the end will likely cause a seg fault and should be removed. And a mexAtExit function should be registered to close the file in the case that the mex function gets cleared before a CLOSE is processed. The data_out[2] = timestamp will also cause a seg fault, should be data_out[0]. That being said, the output variables would be much simpler if mxCreateDoubleScalar were used instead of all the mxGetPr pointer stuff.

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by