Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Segmentation Fault for Mex C!
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Jerome

Posts: 48
Registered: 12/9/11
Segmentation Fault for Mex C!
Posted: Nov 21, 2012 10:50 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

When I am trying to print an element from the matrix, I get a segmentation fault. I am unsure of the issue. Any help is greatly appreciated.

example.m
function example(time,freq)

assignin('base','time',time);
assignin('base','time',freq);
mexgetputvariable;

function.c:
#include <stdio.h>
#include <string.h>
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

char data_name[40], time_name[40], freq_name[40];
mxArray *data_ptr, *time_ptr, *freq_ptr;
mxArray *return_val;
double *var, tvar, *fvar;

/* Check for proper number of input and output arguments */
if (nrhs !=0) {
mexErrMsgTxt("No input arguments required.");
}
if(nlhs > 1){
mexErrMsgTxt("Too many output arguments.");
}

/* Create variable for MATLAB workspace from MEX-file name. */
strcpy(data_name,"");
strcat(data_name,"data");


strcpy(time_name,"");
strcat(time_name,"time");

strcpy(freq_name,"");
strcat(freq_name,"freq");


/* Retrieve the variable from the workspace. */
data_ptr=mxCreateDoubleMatrix(621,1176,mxCOMPLEX); /* size of 1x1 (scalar) */
data_ptr = mexGetVariable("base", data_name);


time_ptr=mxCreateDoubleMatrix(621,1,mxCOMPLEX);
time_ptr = mexGetVariable("base", time_name);


freq_ptr=mxCreateDoubleMatrix(621,1,mxREAL);
freq_ptr = mexGetVariable("base", freq_name);

if (data_ptr == NULL) {
mexPrintf("Variable has not been declared in the workspace.\n");
return;
}

var=mxGetPr(data_ptr)[0];
tvar=mxGetPr(time_ptr)[0];
fvar=mxGetPr(freq_ptr)[0];


/* Display results. */
mexPrintf("Variable was declared at the command prompt as %f", fvar[0]);


mexPutVariable("base", data_name, data_ptr);
mexPutVariable("base", time_name, time_ptr);
mexPutVariable("base", time_name, freq_ptr);


mxDestroyArray(data_ptr);
}



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.