Our goal is to run/Applications/MATLAB _r2013a.app/extern/examples/eng_mat/engdemo. cpp.
1) modify the mac environment variable $ PATH & $ DYLD_LIBRARY_PATH
Terminal:
export PATH='/Applications/MATLAB_R2013a.app/bin':$PATHecho $PATHexport DYLD_LIBRARY_PATH='/Applications/MATLAB_R2013a.app/bin/maci64:/Applications/MATLAB_R2013a.app/sys/os/maci64':$DYLD_LIBRARY_PATHecho $DYLD_LIBRARY_PATH
The echo command is used to view the modified value of $ PATH & $ DYLD_LIBRARY_PATH. In this way, you can modify $ PATH & $ DYLD_LIBRARY_PATH under the terminal, but you also need to add the environment variable you want to add to your home directory for Permanent modification. profile or. bash_profile. If it exists, add it. If not, generate one. PS: mac and linux terminals generally use bash for parsing. After bash finishes reading the/etc/profile of the overall environment variable and calls other configuration files, it then reads the User-Defined personal configuration file. Bash reads three types of files:
~ /. Bash_profile ~ /. Bash_login ~ /. Profile
In fact, bash is a read-only file, and the read order is in the above Order. That is to say, reading bash_profile will not be followed by reading it. If bash_profile does not exist, the following will be available.
OK. After the profile is modified, log out and re-login will modify $ PATH and $ DYLD_LIBRARY_PATH.
2) Run demo
cd /Applications/MATLAB_R2013a.app/extern/examples/eng_matg++ -o engdemo_terminal engdemo.cpp -I/Applications/MATLAB_R2013a.app/extern/include/ -L/Applications/MATLAB_R2013a.app/bin/maci64 -leng -lmx -lm -lmat -lut -lstdc++
For different versions of matlab, modify the include and link directories.
You can see several dynamic link libraries of the link. dylib, equivalent to windows dll), eng (libeng. dylib), mx (libmx. dylib), m (libm. dylib), mat (libmat. dylib), ut (libut. dylib), stdc ++ (Standard Link Library) in/usr/lib/AND/Applications/MATLAB _r2013a.app/bin/maci64 respectively.
Compiled and run:
./engdemo_terminal
Result:
Run the shell code together as follows:
cd /Applications/MATLAB_R2013a.app/extern/examples/eng_matexport PATH='/Applications/MATLAB_R2013a.app/bin':$PATHecho $PATHexport DYLD_LIBRARY_PATH='/Applications/MATLAB_R2013a.app/bin/maci64:/Applications/MATLAB_R2013a.app/sys/os/maci64':$DYLD_LIBRARY_PATHecho $DYLD_LIBRARY_PATHg++ -o engdemo_terminal engdemo.cpp -I/Applications/MATLAB_R2013a.app/extern/include/ -L/Applications/MATLAB_R2013a.app/bin/maci64 -leng -lmx -lm -lmat -lut -lstdc++./engdemo_terminal
--------------------------------------------
2. xcode running routine
1) configure the project attributes as follows: (the project name has not been changed. It was originally used to test mysql. Do not doubt that I am wrong ...... Token)
Include:
Library:
Link: (symbolic link)
Dynamic Link Library:
Copy the. dyilb files under/Applications/MATLAB _ r2013a.app/bin/maci64 to the target folder. Otherwise, an error such
dyld: Library not loaded: @rpath/libeng.dylib Referenced from: /Users/mba/Library/Developer/Xcode/DerivedData/mysqlTest-enzwyvjtfdmbfnebxteublhyjhrd/Build/Products/Release/mysqlTest Reason: image not found
Or
dyld: Library not loaded: @rpath/libut.dylib Referenced from: /Users/mba/Library/Developer/Xcode/DerivedData/mysqlTest-enzwyvjtfdmbfnebxteublhyjhrd/Build/Products/Release/mysqlTest Reason: image not found
In addition, note that the fexport. map, Mexico function. map, and Mexico library. map Files under/Applications/maid/maci64 are not available.
2) Change the Debug mode to Release.
Because the interfaces provided by matlab are release, our project should be changed to release accordingly.
--------------------------------------------
3. kmeans of matlab under C
The Code involves the creation of the matlab engine, the creation of the mex type, the conversion between the C basic data type (double) and the matlab mx type, and how to use kmeans for clustering.
//// Main. cpp // mysqlTest /// Created by Rachel on 13-12-6. // Copyright (c) 2013 ZJU. all rights reserved. // # include
# Include
# Include
# Include engine. h # define BUFSIZE 256int main () {printf (test); Engine * ep; // matlab enginemxArray * T = NULL, * result = NULL; char buffer [BUFSIZE + 1]; double time [10] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0};/** Call engOpen with a NULL string. this starts a MATLAB process * on the current host using the command matlab. */if (! (Ep = engOpen () {fprintf (stderr, Can't start MATLAB engine); return EXIT_FAILURE;} t = mxCreateDoubleMatrix (1, 10, mxREAL ); // transform double to mxArray (double of C ++ to mxArray) memcpy (void *) mxGetPr (T), (void *) time, sizeof (time )); engPutVariable (ep, T, T); // input matlab // transform mxArray to double (mxArray to double) memcpy (void *) time, (void *) mxGetPr (T), sizeof (mxGetPr (T) * 10); // note that the length of time must be multiplied by the buffer [BUFSIZE] = ''; engOutputBuffer (ep, buffer, BUFSIZE); engEvalString (ep, D = kmeans (T, 3); mxDestroyArray (T); mxDestroyArray (result); return EXIT_SUCCESS ;}
--------------------------------------------
4. Use XCode to precisely locate exceptions
Finally, let's talk about the xcode debugging tips and how to locate errors more accurately. During debugging, many strange errors are reported, and you do not know where the errors occur. In this case, xcode can set the breakpoint as the error location: