Trouble building mex function from .c file

14 views (last 30 days)
I have some old c code that someone made a while ago that I'm trying to use in a MATLAB project so I'm trying to convert the c functions to mex functions. I'm new to both c and mex functions, but I've managed to get the compiler installed (Xcode 7 on Mac OSX) and I've written what I thought would let me build the mex function, but I keep getting the following error:
Error using mex
/C_GAmodel/GAmodel.c:108:22: error: passing 'double *' to
parameter of incompatible type 'double'; dereference with *
AircraftModel_(j,Swing, Wspan, FLength, FDiameter, Vel, GTOW, LoverD, BendStress,
Range, Vstall, Climbrate);
^~~~~
*
/C_GAmodel/decfunc.h:3:35: note: passing argument to parameter
here
void AircraftModel_(double, double, double,double, double, double, double *,double *,
double *, double *, double *, double*);
^
1 error generated.
You can see in this error a line of code declaring the function AircraftModel_. The code I've written to try to generate the mex function is:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* Inputs */
double *Swing;
double *Wspan;
double *FLength;
double *FDiameter;
double *Vel;
/* Outputs */
double *GTOW;
double *LoverD;
double *BendStress;
double *Range;
double *Vstall;
double *Climbrate;
int j;
/*
Swing= 14.3; // Wing area
Wspan=10.5; // Wing Span
FLength=8.22; // Fuselage Length
FDiameter=1.5; // Fuselahge Diameter
Vel=40.36; // Cruise Velocity
*/
j=0 ; // Calculates everything
/* Check for proper number of arguments. */
if(nrhs!=5) {
mexErrMsgIdAndTxt( "MATLAB:GAmodel:invalidNumInputs",
"Five inputs required.");
} else if(nlhs!=6) {
mexErrMsgIdAndTxt( "MATLAB:GAmodel:maxlhs",
"Six output arguments required.");
}
/* Create matrices for the return arguments. */
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[3] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[4] = mxCreateDoubleMatrix(1, 1, mxREAL);
plhs[5] = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to each input and output. */
Swing = mxGetPr(prhs[0]);
Wspan = mxGetPr(prhs[1]);
FLength = mxGetPr(prhs[2]);
FDiameter = mxGetPr(prhs[3]);
Vel = mxGetPr(prhs[4]);
GTOW = mxGetPr(plhs[0]);
LoverD = mxGetPr(plhs[1]);
BendStress = mxGetPr(plhs[2]);
Range = mxGetPr(plhs[3]);
Vstall = mxGetPr(plhs[4]);
Climbrate = mxGetPr(plhs[5]);
/* call the C subroutine */
AircraftModel_(j,Swing, Wspan, FLength, FDiameter, Vel, GTOW, LoverD, BendStress, Range, Vstall, Climbrate);
}
And the subroutine I'm trying to call is declared as:
void AircraftModel_(
// Function to be evaluated
double j,
// design variables
double Swing,
double Wspan,
double FLength,
double FDiameter,
double Vel,
// system metrics
double *GTOW,
double *LoverD,
double *BendStress,
double *Range,
double *Vstall,
double *Climbrate
)
{<function body>}
Any help would be appreciated, thanks in advance!

Accepted Answer

James Tursa
James Tursa on 16 Mar 2016
Edited: James Tursa on 16 Mar 2016
Your prototype declares the first six arguments as double, not pointer_to_double:
void AircraftModel_(double, double, double,double, double, double, double *,double *,
double *, double *, double *, double*);
So you need to pass in doubles for these, not pointers. E.g. change your call to this (to dereference your input pointer_to_double variables to double variables):
AircraftModel_(j, *Swing, *Wspan, *FLength, *FDiameter, *Vel, GTOW, LoverD, BendStress, Range, Vstall, Climbrate);
  1 Comment
Callen Brooks
Callen Brooks on 16 Mar 2016
Thanks, this fixed it! Sorry for the rookie mistakes in C, haha, but now that the mex function wrote properly I can go back to working in MATLAB for the time being.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!