Adding C file into Matlab routine

1 次查看(过去 30 天)
Hello,
I am wondering how to integrate a C file into a matlab routine. I know you have to use "mex" in the matlab code, i.e. "mex -O file.c" , and then I call the function. However, I think I need to add something at the beginning of my C file in order to use it with mex? From what I gather, I have to use a mexFunction.. The code seems to compile, but then Matlab crashes every time.
Code:
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double px,py,pz,p1x,p1y,p1z,p2x,p2y,p2z;
double bx,by,bz,a,MU,NU;
double sxx,syy,szz,sxy,syz,sxz;
px=mxGetScalar(prhs[0]);
py=mxGetScalar(prhs[1]);
pz=mxGetScalar(prhs[2]);
p1x=mxGetScalar(prhs[3]);
p1y=mxGetScalar(prhs[4]);
p1z=mxGetScalar(prhs[5]);
p2x=mxGetScalar(prhs[6]);
p2y=mxGetScalar(prhs[7]);
p2z=mxGetScalar(prhs[8]);
bx=mxGetScalar(prhs[9]);
by=mxGetScalar(prhs[10]);
bz=mxGetScalar(prhs[11]);
a=mxGetScalar(prhs[12]);
MU=mxGetScalar(prhs[13]);
NU=mxGetScalar(prhs[14]);
*mxGetPr(plhs[0])=sxx;
*mxGetPr(plhs[1])=syy;
*mxGetPr(plhs[2])=szz;
*mxGetPr(plhs[3])=sxy;
*mxGetPr(plhs[4])=syz;
*mxGetPr(plhs[5])=sxz;
}
static void StressDueToSeg(double px, double py, double pz,
double p1x, double p1y, double p1z,
double p2x, double p2y, double p2z,
double bx, double by, double bz,
double a, double MU, double NU,
double *sxx, double *syy, double *szz,
double *sxy, double *syz, double *sxz)
{ .... stuff...
*sxx = bla bla;
*syy = bla bla;
*szz = bla bla;
*sxy = bla bla;
*syz = bla bla;
*sxz = bla bla;
return;
}
In verbose mode,
**************************************************************************
Warning: Neither -compatibleArrayDims nor -largeArrayDims is selected.
Using -compatibleArrayDims. In the future, MATLAB will require
the use of -largeArrayDims and remove the -compatibleArrayDims
option. For more information, see:
http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html
**************************************************************************
-> mexopts.sh sourced from directory (DIR = $PREF_DIR)
FILE = /Users/Francesco/.matlab/R2012b/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /Applications/MATLAB_R2012b.app
-> CC = llvm-gcc-4.2
-> CC flags:
CFLAGS = -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -mmacosx-version-min=10.5 -fexceptions
CDEBUGFLAGS = -g
COPTIMFLAGS = -O2 -DNDEBUG
CLIBS = -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
arguments = -DMX_COMPAT_32
-> CXX = llvm-g++-4.2
-> CXX flags:
CXXFLAGS = -fno-common -no-cpp-precomp -fexceptions -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -mmacosx-version-min=10.5
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O2 -DNDEBUG
CXXLIBS = -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
FFLAGS = -fexceptions -m64 -fbackslash
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin
arguments = -DMX_COMPAT_32
-> LD = llvm-gcc-4.2
-> Link flags:
LDFLAGS = -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2012b.app/extern/lib/maci64/mexFunction.map
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexmaci64
arguments =
-> LDCXX =
-> Link flags:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments =
----------------------------------------------------------------
-> llvm-gcc-4.2 -c -I/Applications/MATLAB_R2012b.app/extern/include -I/Applications/MATLAB_R2012b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -mmacosx-version-min=10.5 -fexceptions -DMX_COMPAT_32 -O2 -DNDEBUG "StressDueToSeg.c"
-> llvm-gcc-4.2 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/ -mmacosx-version-min=10.5 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2012b.app/extern/lib/maci64/mexFunction.map -o "StressDueToSeg.mexmaci64" StressDueToSeg.o -L/Applications/MATLAB_R2012b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
The error log that I get when it crashes is:
MATLAB crash file:/Users/Francesco/matlab_crash_dump.919-1:
------------------------------------------------------------------------
Segmentation violation detected at Wed Apr 10 20:02:43 2013
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled
Current Visual : None
Default Encoding: ISO-8859-1
MATLAB Root : /Applications/MATLAB_R2012b.app
MATLAB Version : 8.0.0.783 (R2012b)
Operating System: Darwin 12.3.0 Darwin Kernel Version 12.3.0: Sun Jan 6 22:37:10 PST 2013; root:xnu-2050.22.13~1/RELEASE_X86_64 x86_64
Processor ID : x86 Family 6 Model 58 Stepping 9, GenuineIntel
Virtual Machine : Java 1.6.0_43-b01-447-11M4203 with Apple Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : No active display
Fault Count: 1
Abnormal termination:
Segmentation violation
Register State (from fault):
RAX = 0000000000000000 RBX = 0000000000000000
RCX = 0000000000000000 RDX = 0000000000000000
RSP = 637365636e617246 RBP = 2f73726573552f00
RSI = 73657274532f6566 RDI = 666f795f64336464
R8 = 0000000000000000 R9 = 0000000000000000
R10 = 8000000000000000 R11 = 0000000000000000
R12 = 00000001107f0a40 R13 = 000000010af6f000
R14 = 00000001107f0a40 R15 = 000000010af6ff0b
RIP = 0000000000000000 RFL = 0000000133b435d0
CS = 00000001107f0a80 FS = 000000010550aeca GS = 00000001107f0b70
Stack Trace (from fault):
[ 0] 0x00000001000e4e46 /Applications/MATLAB_R2012b.app/bin/maci64/libmwfl.dylib+00036422 _ZN2fl4diag15stacktrace_base7captureERKNS0_14thread_contextEm+000150
[ 1] 0x00000001000e5af1 /Applications/MATLAB_R2012b.app/bin/maci64/libmwfl.dylib+00039665 fl_diag_terminate+000321
[ 2] 0x00000001000e7c34 /Applications/MATLAB_R2012b.app/bin/maci64/libmwfl.dylib+00048180 _ZN2fl4diag13terminate_logEPKcRKNS0_14thread_contextE+000100
[ 3] 0x00000001008e125e /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00315998 mnTrapCtrlc+000254
[ 4] 0x00000001008e2d24 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00322852 _Z32mnRunPathDependentInitializationv+003492
[ 5] 0x00000001008e313d /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00323901 _Z32mnRunPathDependentInitializationv+004541
[ 6] 0x00000001008e37f5 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00325621 _Z32mnRunPathDependentInitializationv+006261
[ 7] 0x00000001008e3945 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00325957 mnFatalSignalHandler+000149
[ 8] 0x00007fff95ec094a /usr/lib/system/libsystem_c.dylib+00010570 _sigtramp+000026
[ 9] 0x00000000fffffff5 /usr/lib/system/libsystem_c.dylib+18446603342295867381
[ 10] 0x000000010af6ff0b /Users/Francesco/Documents/Dropbox/DPhil/MATLAB/dd3d_yoffe/StressDueToSeg.mexmaci64+00003851 mexFunction+000155
[ 11] 0x000000010550aeca /Applications/MATLAB_R2012b.app/bin/maci64/libmex.dylib+00065226 mexRunMexFile+000090
[ 12] 0x0000000105506a29 /Applications/MATLAB_R2012b.app/bin/maci64/libmex.dylib+00047657 _ZN7Mfh_mex30runMexFileWithSignalProtectionEiPP11mxArray_tagiS2_+000137
[ 13] 0x0000000105508038 /Applications/MATLAB_R2012b.app/bin/maci64/libmex.dylib+00053304 _ZN7Mfh_mex13dispatch_fileEiPP11mxArray_tagiS2_+000232
[ 14] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 15] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 16] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 17] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 18] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 19] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 20] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 21] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 22] 0x0000000100b277d9 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00772057 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+001113
[ 23] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 24] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 25] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 26] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 27] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 28] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 29] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 30] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 31] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 32] 0x0000000100b277d9 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00772057 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+001113
[ 33] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 34] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 35] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 36] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 37] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 38] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 39] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 40] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 41] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 42] 0x0000000100b277d9 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00772057 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+001113
[ 43] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 44] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 45] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 46] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 47] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 48] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 49] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 50] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 51] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 52] 0x0000000100b24361 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00758625 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+012273
[ 53] 0x0000000100b29c1c /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00781340 _Z18protected_inInterp12inDebugCheckii7opcodesPV15inPcodeNest_tagPl+000140
[ 54] 0x0000000100b2614e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00766286 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+019934
[ 55] 0x0000000100b27630 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00771632 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+000688
[ 56] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 57] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 58] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 59] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 60] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 61] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 62] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 63] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 64] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 65] 0x0000000100b277d9 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00772057 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+001113
[ 66] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 67] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 68] 0x0000000100d8b5b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03278258 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000914
[ 69] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 70] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 71] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 72] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 73] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 74] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 75] 0x0000000100b277d9 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00772057 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+001113
[ 76] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 77] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 78] 0x0000000100b5734d /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00967501 _ZN12InterpBridge7CallFcnEiPP11mxArray_tagiS2_PKc+000349
[ 79] 0x0000000100aab1af /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00262575 inFullFevalFcn+000111
[ 80] 0x00000001009a03b4 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00074676 _ZN11Mfh_builtin11dispatch_mfEiPP11mxArray_tagiS2_+000084
[ 81] 0x0000000100991e25 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00015909 _ZN13Mfh_MATLAB_fn11dispatch_fhEiPP11mxArray_tagiS2_+000501
[ 82] 0x0000000100d8b43d /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03277885 _ZN20ResolverFunctionDesc12CallFunctionEiPP11mxArray_tagiS2_+000541
[ 83] 0x0000000100d9287f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03307647 _ZN8Resolver13CallMFunctionEiiP10_m_operandP17m_operand_storageiS1_S3_Pi+001679
[ 84] 0x0000000100d934e6 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+03310822 _Z22inResolveMFunctionCallP16_m_function_desciiP10_m_operandP17m_operand_storageiS2_S4_PiP13inMarshalTypeiPK19mpsTypeSequenceNlhsPFP11mxArray_tagiE+000598
[ 85] 0x0000000100bc9b4f /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01436495 _ZN9accelImpl13MFunctionCallEPP8_accelOp+000527
[ 86] 0x0000000100bf4b7e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612670 _ZN9accelImpl4ExecEv+000286
[ 87] 0x0000000100bf4c3b /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01612859 _ZNK9accelCode4CallEP13inMarshalTypePi+000091
[ 88] 0x0000000100d39f16 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+02944790 _ZN5inJit17ExecuteHotSegmentEP15_inJitAccelInfoP7opcodesPiPl+001654
[ 89] 0x0000000100b24361 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00758625 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+012273
[ 90] 0x0000000100b29c1c /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00781340 _Z18protected_inInterp12inDebugCheckii7opcodesPV15inPcodeNest_tagPl+000140
[ 91] 0x0000000100b2614e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00766286 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+019934
[ 92] 0x0000000100b27630 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00771632 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+000688
[ 93] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[ 94] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[ 95] 0x0000000100b76a5a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01096282 _Z19inDispatchFromStackiPKcii+001034
[ 96] 0x0000000100b02979 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00620921 inCallFcnFromReference+000265
[ 97] 0x0000000100b23125 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00753957 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+007605
[ 98] 0x0000000100b29c1c /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00781340 _Z18protected_inInterp12inDebugCheckii7opcodesPV15inPcodeNest_tagPl+000140
[ 99] 0x0000000100b2614e /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00766286 _Z16inEnterDebugModeN5boost8functionIFvvEEEb+019934
[100] 0x0000000100b27630 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00771632 _Z26inExecuteMFunctionOrScriptP6Mfh_mpb+000688
[101] 0x0000000100b9482a /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01218602 _Z10inRunMfileiPP11mxArray_tagiS1_P6Mfh_mpP15inWorkSpace_tag+001530
[102] 0x00000001009dab43 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_dispatcher.dylib+00314179 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+000499
[103] 0x0000000100b62904 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+01014020 _Z23inEvalPcodeHeaderToWordP15_memory_contextiPP11mxArray_tagP12_pcodeheaderP6Mfh_mpj+000260
[104] 0x0000000100b1c162 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00725346 _Z25in_local_call_with_setjmpIN5boost3_bi6bind_tIvPFvP15_memory_contextPiPP11mxArray_tagP12_pcodeheaderjENS1_5list5INS1_5valueIS4_EENS0_3argILi1EEENSG_ILi2EEENSE_ISA_EENSE_IiEEEEEEE17inExecutionStatusT_S5_S8_b+000082
[105] 0x0000000100b187f5 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00710645 _ZN7EvalLog6setLogEiPP11mxArray_tagiS2_+003381
[106] 0x0000000100b18b75 /Applications/MATLAB_R2012b.app/bin/maci64/libmwm_interpreter.dylib+00711541 _ZN7EvalLog6setLogEiPP11mxArray_tagiS2_+004277
[107] 0x00000001063fd58e /Applications/MATLAB_R2012b.app/bin/maci64/libmwbridge.dylib+00058766 _Z28evalCommandWithLongjmpSafetyPKc+000094
[108] 0x00000001063fdd8b /Applications/MATLAB_R2012b.app/bin/maci64/libmwbridge.dylib+00060811 _Z28evalCommandWithLongjmpSafetyRKSbItSt11char_traitsItESaItEE+000043
[109] 0x00000001063fe81d /Applications/MATLAB_R2012b.app/bin/maci64/libmwbridge.dylib+00063517 _Z8mnParserv+001021
[110] 0x00000001008c4139 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00196921 _ZN11mcrInstance30mnParser_on_interpreter_threadEv+000041
[111] 0x000000010089e76b /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00042859 _ZN3mcr7runtime17InterpreterThread4Impl17InvocationRequest3runEv+000523
[112] 0x000000010089e925 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00043301 _ZN3mcr7runtime17InterpreterThread4Impl26invocation_request_handlerEl+000053
[113] 0x000000010042f574 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00107892 _ZN10eventqueue18UserEventQueueImpl5flushEv+000756
[114] 0x000000010042f79a /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00108442 _ZN10eventqueue8ReadPipeEib+000026
[115] 0x000000010042df26 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00102182 _ZN10eventqueue18UserEventQueueImpl9selectFcnEb+000326
[116] 0x0000000107d23a9d /Applications/MATLAB_R2012b.app/bin/maci64/libmwuix.dylib+00170653 _ZN21uix_nodisplay_ppeHook16pollingDuringFcnEb+000045
[117] 0x00000001004d71b2 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00795058 _ZSt8for_eachIN9__gnu_cxx17__normal_iteratorIPN5boost8weak_ptrIN4sysq10ws_ppeHookEEESt6vectorIS6_SaIS6_EEEENS4_8during_FIS6_NS2_10shared_ptrIS5_EEEEET0_T_SH_SG_+000162
[118] 0x00000001004d8dab /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00802219 _ZN4sysq12ppe_for_eachINS_8during_FIN5boost8weak_ptrINS_10ws_ppeHookEEENS2_10shared_ptrIS4_EEEEEET_RKS9_+000203
[119] 0x00000001004d592c /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00788780 _ZN4sysq19ppePollingDuringFcnEb+000092
[120] 0x00000001004d5ac7 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00789191 _ZN4sysq11ppeMainLoopEiib+000119
[121] 0x00000001004d5c94 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00789652 _ZN4sysq11ppeLoopIfOKEiib+000132
[122] 0x00000001004d5e34 /Applications/MATLAB_R2012b.app/bin/maci64/libmwservices.dylib+00790068 _ZN4sysq20processPendingEventsEiib+000132
[123] 0x000000010089ea9f /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00043679 _ZN3mcr7runtime17InterpreterThread4Impl14process_eventsERKN5boost10shared_ptrIS2_EE+000223
[124] 0x000000010089f2ca /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00045770 _ZN3mcr7runtime17InterpreterThread4Impl3runERKN5boost10shared_ptrIS2_EEPNS2_12init_contextE+000282
[125] 0x0000000100897176 /Applications/MATLAB_R2012b.app/bin/maci64/libmwmcr.dylib+00012662 _Z26run_init_and_handle_eventsPv+000054
[126] 0x00007fff95ed27a2 /usr/lib/system/libsystem_c.dylib+00083874 _pthread_start+000327
[127] 0x00007fff95ebf1e1 /usr/lib/system/libsystem_c.dylib+00004577 thread_start+000013
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
Any advice would be greatly appreciated. This is the first time I try to incorporate my own C file into a Matlab routine.
Kind Regards,
F

采纳的回答

James Tursa
James Tursa 2013-4-10
编辑:James Tursa 2013-4-10
The difference is that in your original code you did not create any variables in the plhs[0], plhs[1], etc spots before you tried to access them with these lines:
*mxGetPr(plhs[0])=sxx;
*mxGetPr(plhs[1])=syy;
*mxGetPr(plhs[2])=szz;
*mxGetPr(plhs[3])=sxy;
*mxGetPr(plhs[4])=syz;
*mxGetPr(plhs[5])=sxz;
So plhs[0] etc all contain invalid pointers at that point in your code, hence the crash.
In your newer posted code, the plhs[0] etc arrays are created prior to accessing them via this:
for (i=0; i<nlhs; i++) {
plhs[i]=mxCreateDoubleMatrix(1,1,mxREAL);}
You don't show it, but I am assuming you have this necessary line at the top of your code?
#include "mex.h"
  3 个评论
James Tursa
James Tursa 2013-4-10
Please post an example of how you would like to call the mex function from the MATLAB level (including the looping), and it should be pretty easy. Basically, instead of all those mxGetScalar calls you do mxGetPr calls and then wrap the rest of the code inside a loop. The StressDueToSeg calling syntax changes slightly also. I can show you this once you post the MATLAB level syntax.
Fran
Fran 2013-4-12
编辑:Fran 2013-4-12
Hi there!
Here is the Matlab code where I call the C file :)
Thank you for your help!
% inputs:x(dim,1:3) a list of positions where you want the stress calculated
% x1(nseg,1:3) a list of starting positions of the dislocations segments
% x2(nseg,1:3) a list of ending positions of the dislocation segments
% b(nseg,1:3) a list of burgers vectors associated with the segments going from x1 to x2
% a=core width
% mu= shear modulus
% nu=poisson's ratio
%
% outputs:Stress(dim,1:6) stresses at the field points requested it will calculate the field point stress as a sum of all the segments
% the components of the stress are organized in the following fashion
% Stress(i,:)=[s_11 s_22 s_33 s_12 s_23 s_13]
sx=size(x,1);
nseg=size(x1,1);
stress_grid = zeros(sx,6);
for i = 1:sx
stress_point = zeros(1,6);
px = x(i,1);
py = x(i,2);
pz = x(i,3);
for j = 1:nseg
p1x = x1(j,1);
p1y = x1(j,2);
p1z = x1(j,3);
p2x = x2(j,1);
p2y = x2(j,2);
p2z = x2(j,3);
bx = b(j,1);
by = b(j,2);
bz = b(j,3);
[sxx, syy, szz,...
sxy, syz, sxz] = StressDueToSeg(px, py, pz,...
p1x, p1y, p1z,...
p2x, p2y, p2z,...
bx, by, bz,...
a, MU, NU);
stress = horzcat(sxx,syy,szz,sxy,syz,sxz);
stress_point = stress_point + stress;
end
stress_grid(i,:) = stress_point;
end

请先登录,再进行评论。

更多回答(1 个)

Fran
Fran 2013-4-10
Hello,
I found another code and copied the structure and it seems to work. Would someone please clarify what it's doing?
static void StressDueToSeg(double px, double py, double pz,
double p1x, double p1y, double p1z,
double p2x, double p2y, double p2z,
double bx, double by, double bz,
double a, double MU, double NU,
double *sxx, double *syy, double *szz,
double *sxy, double *syz, double *sxz);
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double px,py,pz,p1x,p1y,p1z,p2x,p2y,p2z;
double bx,by,bz,a,MU,NU;
double sxx,syy,szz,sxy,syz,sxz;
int i;
px=mxGetScalar(prhs[0]);
py=mxGetScalar(prhs[1]);
pz=mxGetScalar(prhs[2]);
p1x=mxGetScalar(prhs[3]);
p1y=mxGetScalar(prhs[4]);
p1z=mxGetScalar(prhs[5]);
p2x=mxGetScalar(prhs[6]);
p2y=mxGetScalar(prhs[7]);
p2z=mxGetScalar(prhs[8]);
bx=mxGetScalar(prhs[9]);
by=mxGetScalar(prhs[10]);
bz=mxGetScalar(prhs[11]);
a=mxGetScalar(prhs[12]);
MU=mxGetScalar(prhs[13]);
NU=mxGetScalar(prhs[14]);
StressDueToSeg(px, py, pz,p1x, p1y, p1z,
p2x, p2y, p2z, bx, by, bz,
a, MU, NU,
&sxx, &syy, &szz, &sxy, &syz, &sxz);
for (i=0; i<nlhs; i++) {
plhs[i]=mxCreateDoubleMatrix(1,1,mxREAL);}
*mxGetPr(plhs[0])=sxx;
*mxGetPr(plhs[1])=syy;
*mxGetPr(plhs[2])=szz;
*mxGetPr(plhs[3])=sxy;
*mxGetPr(plhs[4])=syz;
*mxGetPr(plhs[5])=sxz;
}
void StressDueToSeg(double px, double py, double pz,
double p1x, double p1y, double p1z,
double p2x, double p2y, double p2z,
double bx, double by, double bz,
double a, double MU, double NU,
double *sxx, double *syy, double *szz,
double *sxy, double *syz, double *sxz)
{ ... stuff
return;
}
Thank you again,
F

类别

Help CenterFile Exchange 中查找有关 Server Management 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by