I have mex wrappers to Fortran codes. I want to call those mex functions in Matlab in Windows 64-bit computer. So the question is: do I need to setup Fortran compiler with the Matlab to just call the mex functions? Or, would it be as simple as calling the function as any Matlab function? the latter didn't work.
To be able to "just call the mex functions" FORTRAN source code (files with a .F extension) needs to be compiled into an executable for your computer's architecture. For Windows-64bit, the resultant files will likely have a file extension of either .mexa64 (AMD-64) or .mexw64 (Intel-64). Type mexext in your command window if you are unsure of what kind of CPU you have.
Related
I created a .mex file of a MATLAB file test.m from MATLAB R2012b using MATLAB coder.
I got the output file test_mex.mexa64. I was hoping to use this .mex file in MATLAB 7.5.0 (R2007b) but it shows the following error.
Invalid MEX-file '/work/sreekanthl/test_mex.mexa64': libmwblascompat32.so:
cannot open shared object file: No such file or directory.
How can I make this .mex file work in 7.5.0 (R2007b)?
I agree with David Kelley's answer for general MEX files but there is a special consideration for MATLAB Coder generated MEX files. MATLAB Coder generated MEX code is expected to be forward compatible but generally is not expected to be backward compatible. In other words, you are expected to be able to use such MEX code in a newer release than the one with which it was generated, however using the code in an earlier release cannot be guaranteed to work.
The reason for this is that the MEX code uses various runtime libraries which ship with MATLAB. These libraries advance over time and the MEX file from the newer version may depend on features that the older libraries do not provide, or even on a new library which does not exist in the older release as you are seeing here.
If you want to create MEX code that is compatible with earlier releases, you could generate a standalone target such as a static library or a shared library (LIB or DLL respectively for MATLAB Coder), and either:
Try to use loadlibrary to call the generated code in MATLAB
Write a generic MEX interface for it and compile that MEX code in the older release
These options are more work but should enable you to create a MEX file or shared library usable in an older release.
While TMW says that mex functions are usually compatible between versions, it's not officially supported and quite common to run into issues with it.
The only reliable option is to simply recompile the generated source code that Matlab coder created (i.e., the C code) in the version you want to run it in using the mex function.
I've upgraded my MATLAB to 2014b (on OS X 10.10), and tried to test some old MEX C/C++ codes.
As usual, I run mex -setup from the command window, and I would expect to be provided the chance to overwrite the option file "meshopt.sh".
But now, it seems MATLAB has made some change and I am not allowed to change the MEX configuration, it simply says:
MEX configured to use Xcode with Clang for C language compilation,
...
to choose a different language, select one from the following
mex -setup C++
mex -setup FORTRAN
So, is it true that the old ways to modify "mexopts.sh" are no longer feasible?
It seems that the following command works:
mex -setup my_mexopts.sh
however, there's some warning information,
Legacy MEX infrastructure is provided for compatibility; it will be removed in a future version of MATLAB.
Apart from that, everything looks fine, except I got some warning at compile time seems due to compiler version(gcc 4.9.1) on my current system(OS X 10.10).
gcc: warning: couldn't understand kern.osversion '14.0.0
When I run mex -setup C, I see information like the following:
Microsoft Visual C++ 2008 (C) mex -setup:H:\MATLAB\R2014b\bin\win64\mexopts\msvc2008.xml C
Microsoft Visual C++ 2010 (C) mex -setup:H:\MATLAB\R2014b\bin\win64\mexopts\msvc2010.xml C
Microsoft Visual C++ 2012 (C) mex -setup:H:\MATLAB\R2014b\bin\win64\mexopts\msvc2012.xml C
The mex part is actually a link you can click to choose that configuration. So, to make your own:
Choose an xml as a template and copy it. (e.g I'd copy H:\MATLAB\R2014b\bin\win64\mexopts\msvc2012.xml to %AppData%\MathWorks\MATLAB\R2014b\mex_C++_win64_custom.xml).
Make the edits in the XML file, probably in the <vars> tag (e.g. modify COMPFLAGS or CFLAGS whatever variable has your compiler options) or the <env> tag (e.g. PATH, INCLUDE, LIB, etc.)
Set that to the current configuration:
mex -setup:C:\[YourMATLABAppDataHere]\myCopts.xml C
Note that a good place in Windows to keep settings is under %APPDATA%. For MATLAB R2014b, this would be something like C:\Users\[yourname]\AppData\Roaming\MathWorks\MATLAB\R2014b.
Do the same for C++ or FORTRAN configurations too.
can you give me a basic c++ code that inclue opencv libraries , and can call the matlab engine ?
the file should compile using matlab mex.
(my problem is when I use the engine and compile it with "mex -f optionfile file.cpp"I get error message "cannot include opencv\cv.h" , but using the opencv without the engine its work using the mex file.xpp)
You need Matlab engine if you want to run .m files outside of Matlab command window (e.g. bash, c++ source file, etc.)
In your case, mex program (Matlab External Interface which acts like g++ or other c++ compilers) is required to compile a c++ source file. You must provide mex program a source file which is written according to what Matlab has introduced in its documentation.
If your source code (.cpp file) is including opencv/cv.h file and using one of its methods, you need to link the appropriate library (say libopencv.so, libopencv.dll or etc,) using -l flag. (I use mex foo.cpp -l opencv_core)
I hope it helps, you can provide your source
I am trying to compile a C++ function (found here). I downloaded the requisite packages, installed Microsoft Windows SDK 7.1, linked it to MATLAB using mex -setup, and then unzipped the function packages into my MATLAB path.
When I run the command:
mex -output Faddeeva_w -O Faddeeva_w_mex.cc Faddeeva.cc
I get the error message:
C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: 'Faddeeva_w_mex.cc' not found.
But if I type which Faddeeva_w_mex.cc I get:
C:\Users\craigim\Documents\Code\Matlab\Faddeeva-MATLAB\Faddeeva_w_mex.cc
So MATLAB and the which command have no problems finding the file, but mex is lost. This is my very first attempt at compiling something with mex, so I'm really not sure where to start here. Does mex have a different path variable that I have to set?
I'm using MATLAB 2013a on a Windows 7 64bit machine with the version of SDK 7.1 that was linked from the MATLAB List of supported compilers.
Either cd into the directory containing the files (assuming they both in the same place), or specify full/relative path to the C++ files when invoking mex function
Try putting addpath('C:\Users\craigim\Documents\Code\Matlab\Faddeeva-MATLAB\'); in your m-file. The path that MATLAB uses seems to be internal and independent of whatever your OS is using.
Documentation on addpath here.
And a similar discussion here.
You may also have to set your PATH environment variable to the string returned by the command fullfile(matlabroot,'bin',computer('arch')) in MATLAB. Here are some instructions on setting environment variables for Windows and Mac or Linux
There is also a pretty good overview of building mex-files here although I get the impression that the basics are not going to answer your question.
I want to code and build Matlab mex files on Eclipse IDE. How could I do to include mex compiler as one of the c++/c compilers and build the mex code I have with that compiler on Eclipse?
I don't know what you are trying to say but I'll explain how mex helped me.
Mex is - Matlab Executable this you'd be using to create the functions similar to Matlab functions Ex: function varargout = foo(varargin). This foo will be your foo.c or foo.cpp.
The foo.c or cpp is compiled with the well known C/C++ Compilers mentioned here -http://www.mathworks.in/support/compilers/R2012a/win64.html
This generates a foo.mexw32 or foo.mexw64 in Windows and you can call it like a Matlab Function.
What you can do is write a code in Eclipse using the C compiler it has get it execute and port it to mex functions easily. Select the compilers from mex -setup
I found this tutorial extremely helpful :
Mex Tutorial
and I doubt there is something call "Mex compiler" atleast I've not come across.
I use the tutorial of http://ngoonee.xanga.com/664222870/compiling-mexw32-mex-for-windows-files-using-eclipse-cdt-ide-mingw-gcc-compiler/
Works for me!