이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Use mex files in matlab for MAC
조회 수: 116 (최근 30일)
이전 댓글 표시
I need to use a function designed to provide ARMA-Garch-in-Mean estimation in matlab and I found a code online in a mex file. Thing is I don't know what to do next in order to use this function in matlab. I am a mac user and don't even know how to set mex files in matlab for mac. So, pretty much I need to do a ARMA-Garch-in-Mean estimation and have no clue on how to do it.
Can anyone help? What should I do?
I can give you de code or the link where I found it if it helps...
Thanks in advance, Rui
채택된 답변
Geoff Hayes
2014년 8월 25일
Rui - it may be helpful to attach the files so that we know what you code you have. I suspect that you have at least one m-file that calls one or more MEX functions that are in C-files i.e. files with a c extension (written in the C programming language).
I have a Mac as well, and it is easy to build the MEX-functions from C/C++ as per the documentation found at the link MEX.
To setup, you need a C/C++ compiler. In the MATLAB Command Window, type
mex -setup C
What do you see? When I run this command, I observe
MEX configured to use 'Xcode with Clang' for C language compilation.
You may need to install Xcode in order to get a similar compiler. Once a compiler has been chosen, you can build the MEX-files. Change to the directory that contains the *.c files and type the command (in the MATLAB Command Window) to build the MEX-function
mex functionName.c
where functionName is the name of the function/file. You will either observe errors or see that it built successfully
Building with 'Xcode with Clang'.
MEX completed successfully.
In your folder, there will now be a file similar to functionName.mexmaci64.
Repeat the above for each C-file.
Once completed, you can call each MEX-function from within the MATLAB code as if it were a MATLAB function
% call functionName to do something
Z = functionName(A,B);
so long as you provide inputs (and assign outputs) as directed by the function signature.
Try the above and see what happens!
댓글 수: 22
Rui Ascenso
2014년 8월 26일
Thanks Geoff. Here is the link to the code I've found:
It's code for a function 'garchinmean' which does what I need. and it I think it calls other mex functions indeed as you suggested and these can be found in the following link:
Also, I have installed Matlab_R2013a on OS X 10.9.4 (13E28). which xcode version should I use? Is there a specific one for this software or I can use any?
Geoff Hayes
2014년 8월 26일
Check first to see if you have a compiler already. From the MATLAB Command Window, type
mex -setup C
What do you observe? As for installing Xcode, it will be whatever version is suited for your OS X. The App Store will determine that for you.
Rui Ascenso
2014년 8월 26일
The options files available for mex are:
1: /Applications/MATLAB_R2013a.app/bin/mexopts.sh :
Template Options file for building MEX-files
0: Exit with no changes
And if I choose 1 it tells me:
Overwrite /Users/ruiascenso/.matlab/R2013a/mexopts.sh ([y]/n)?
Does this mean I don't have a compiler yet? I'm downloading xcode.
Geoff Hayes
2014년 8월 26일
What appears now that you have downloaded Xcode? Make sure you check details at this link mac, matlab, compiler.
Rui Ascenso
2014년 8월 27일
So Geoff, I have Xcode and its command line tools installed and I even edited my mexopts.sh according to
in order to setup everything but it's still not working. I always get this warning:
/Applications/MATLAB_R2013a.app/bin/NEW_mexopts.sh is being copied to /Users/ruiascenso/.matlab/R2013a/mexopts.sh
************************************************************************ Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html Building with the -largeArrayDims option enables the new API. ************************************************************************
I don't know what to do..
Geoff Hayes
2014년 8월 27일
Rui - what did you change your mexopts.sh file to? What version of OS X are you using? What command did you run that generated the above warning and message?
On my previous MATLAB installation of R2013a (with OS X 10.8.5), I followed the above link and updated the mexopts.sh in my /Users/geoff/.matlab/R2013a/ directory. The C-section looks like
#----------------------------------------------------------------------------
# StorageVersion: 1.0
# CkeyName: Clang
# CkeyManufacturer: Apple
# CkeyLanguage: C
# CkeyVersion:
# CkeyLinkerName:
# CkeyLinkerVersion:
CC='xcrun -sdk macosx10.8 clang'
## workaround clang defect temporarily use line below SDKROOT='/Developer/SDKs/MacOSX10.6.sdk'
# compute SDK root on the fly
# target 10.8
MW_SDK_TEMP="find `xcode-select -print-path` -name MacOSX10.8.sdk"
MW_SDKROOT=`$MW_SDK_TEMP`
MACOSX_DEPLOYMENT_TARGET='10.8'
ARCHS='x86_64'
CFLAGS="-fno-common -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
CFLAGS="$CFLAGS -fexceptions"
CLIBS="$MLIBS"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'
#
CLIBS="$CLIBS -lstdc++"
# C++keyName: Clang++
# C++keyManufacturer: Apple
# C++keyLanguage: C++
# C++keyVersion:
# C++keyLinkerName:
# C++keyLinkerVersion:
CXX='xcrun -sdk macosx10.8 clang++'
CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
CXXLIBS="$MLIBS -lstdc++"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'
Is that similar to what you have done? For 2014a, I didn't have to do this and it seems to work okay (without the mexopts.sh file in the /Users/geoff/.matlab/R2014a folder).
Rui Ascenso
2014년 8월 27일
I use OS X 10.9.4 and matlab R2013a. I did the same changes so it is actually equal to mine. These were all the commands (step-by-step) I run:
>> mex -setup
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mex -setup' command selects an options file that is
placed in /Users/ruiascenso/.matlab/R2013a and used by default for 'mex'. An options
file in the current working directory or specified on the command line
overrides the default options file in /Users/ruiascenso/.matlab/R2013a.
To override the default options file, use the 'mex -f' command
(see 'mex -help' for more information).
The options files available for mex are:
1: /Applications/MATLAB_R2013a.app/bin/NEW_mexopts.sh :
Template Options file for building MEX-files
2: /Applications/MATLAB_R2013a.app/bin/mexopts.sh :
Template Options file for building MEX-files
0: Exit with no changes
Enter the number of the compiler (0-2): 1
Overwrite /Users/ruiascenso/.matlab/R2013a/mexopts.sh ([y]/n)? y
/Applications/MATLAB_R2013a.app/bin/NEW_mexopts.sh is being copied to /Users/ruiascenso/.matlab/R2013a/mexopts.sh
************************************************************************ Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html Building with the -largeArrayDims option enables the new API. ************************************************************************
Regarding 2014a I have a retina display so I guess I'll have problems with blurry screens (problems that don't seem to have a good fix yet..)
Geoff Hayes
2014년 8월 27일
Rui - I went to my R2013a application, ran it, and observed the same (or almost the same) output when I ran mex -setup:
>> mex -setup
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mex -setup' command selects an options file that is
placed in /Users/geoff/.matlab/R2013a and used by default for 'mex'. An options
file in the current working directory or specified on the command line
overrides the default options file in /Users/geoff/.matlab/R2013a.
To override the default options file, use the 'mex -f' command
(see 'mex -help' for more information).
The options files available for mex are:
1: /Applications/MATLAB_R2013a.app/bin/mexopts.sh :
Template Options file for building MEX-files
0: Exit with no changes
Enter the number of the compiler (0-1):
So I pressed zero and figured that I would try to compile a file. I used the yprime.c example (see attached, just change the extension from txt to c) that comes with MATLAB. In the Command Window (in the directory where I copied the yprime.c), I typed
mex yprime.c
and it failed with the error
In file included from yprime.c:17:
In file included from /Applications/MATLAB_R2013a.app/extern/include/mex.h:58:
In file included from /Applications/MATLAB_R2013a.app/extern/include/matrix.h:294:
/Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:819:9: error: unknown type name 'char16_t' typedef char16_t CHAR16_T;
Do you see the same? There are ways to get around this problem. One solution is to add the flag/directive -std=c++11 to the CXXFLAGS variable of your mexopts.sh file (at line 150, or thereabouts, in the Mac section) and change the extension of your file to yprime.cpp. If that doesn't work, there are other options.
Try compiling your c-files, and see what happens!
Rui Ascenso
2014년 8월 29일
Geoff - I did the changes you suggested and in the end tried the command mex yprime.cpp again but got the same error:
>> mex yprime.cpp In file included from yprime.cpp:17: In file included from /Applications/MATLAB_R2013a.app/extern/include/mex.h:58: In file included from /Applications/MATLAB_R2013a.app/extern/include/matrix.h:294: /Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:819:9: error: unknown type name 'char16_t' typedef char16_t CHAR16_T;
^
1 error generated.
mex: compile of ' "yprime.cpp"' failed.
Error using mex (line 206) Unable to complete successfully.
What would be the alternatives?
Geoff Hayes
2014년 8월 30일
Rui - are you sure that you added that flag in the correct location? I did the same, and was able to compile the attached file without any errors. For alternatives, see this link.
Rui Ascenso
2014년 8월 30일
I have this line now:
CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -std=c++11"
Isn't this the correct way of adding it?
Also I have always 10.8 (e.g."macosx10.8") in all maci64 part. Should I have 10.7 or 10.9 instead?
Geoff Hayes
2014년 8월 30일
I have the same line,
CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -std=c++11"
so that should be fine. I'm using 10.8 only because I'm still running Mountain Lion.
Did you make this change to the mexopts.sh file within your personal directory at /Users/ruiascenso/.matlab/R2013a or /Applications/MATLAB_R2013a.app/bin/mexopts.sh. I would do the latter, then re-run mex -setup, choose this updated file so that the copy of it gets copied to /Users/ruiascenso/.matlab/R2013a.
If that doesn't work, then try adding
#define char16_t uint16_t
before the include statements of yprime.cpp i.e.
#define char16_t uint16_t
#include <math.h>
#include "mex.h"
Rui Ascenso
2014년 8월 30일
Now I got this error:
>> mex yprime.cpp In file included from yprime.cpp:18: In file included from /Applications/MATLAB_R2013a.app/extern/include/mex.h:58: In file included from /Applications/MATLAB_R2013a.app/extern/include/matrix.h:294: /Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:819:9: error: unknown type name 'uint16_t'; did you mean 'uint16_T'? typedef char16_t CHAR16_T; ^~~~~~~~ uint16_T yprime.cpp:16:19: note: expanded from macro 'char16_t' #define char16_t uint16_t ^ /Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:257:19: note: 'uint16_T' declared here typedef UINT16_T uint16_T; ^ 1 error generated.
mex: compile of ' "yprime.cpp"' failed.
Error using mex (line 206) Unable to complete successfully.
but somehow I feel I'm getting closer to solve the problem...
Geoff Hayes
2014년 8월 30일
편집: Geoff Hayes
2014년 8월 30일
Try
#define char16_t uint16_T
or replace the above with
#include <typeinfo>
which may include the char16_t.
Rui Ascenso
2014년 8월 30일
Geoff I think it worked. For the first time I didn't get an error message and it created a file called yprime.mexmaci64. Does it means it worked?
How do I call a function now? Let's say the function for Garch-in-Mean I found online? Should I save it as .cpp file and then just use the funtion?
Geoff Hayes
2014년 8월 30일
Rui - try the following
yprime(0,1:4)
Your answer should be
ans =
2 8.9685 4 -1.0947
As for the Garch stuff, rename of cpp and add whatever you did for the yprime.cpp.
Rui Ascenso
2014년 8월 30일
It worked. Thank you very much Geoff. I truly appreciated the time you spend with me. Great great help!
zaonly
2015년 2월 17일
Hi Geoff Haye and Rui Ascenso,
This Conversation was help me to solve my problem about mex -setup compiles.
I do my last project with Mindwave Mobile and Matlab R2014b for Mac (OS 10.10.2). There's one question i want to ask to both of you, after setup mex, i still cant running my code. This is the result :
>> mex -setup
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mexsh -setup' command selects an options file that is
placed in /Users/ahmadazhari/.matlab/R2014b and used by default for 'mexsh'. An options
file in the current working directory or specified on the command line
overrides the default options file in /Users/ahmadazhari/.matlab/R2014b.
To override the default options file, use the 'mexsh -f' command
(see 'mexsh -help' for more information).
The options files available for mexsh are:
1: /Applications/MATLAB_R2014b.app/bin/mexopts.sh :
Template Options file for building MEX-files
0: Exit with no changes
Enter the number of the compiler (0-1):
1
Overwrite /Users/ahmadazhari/.matlab/R2014b/mexopts.sh ([y]/n)?
y
/Applications/MATLAB_R2014b.app/bin/mexopts.sh is being copied to
/Users/ahmadazhari/.matlab/R2014b/mexopts.sh
**************************************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
Building with the -largeArrayDims option enables the new API.
**************************************************************************
>>
I get stuck on this code :
>> readRAW
Error using loadlibrary (line 440)
There was an error loading the library "/Users/ahmadazhari/Documents/MATLAB/matlabdll/Thinkgear.dll"
dlopen(/Users/ahmadazhari/Documents/MATLAB/matlabdll/Thinkgear.dll, 6): no suitable image found. Did find:
/Users/ahmadazhari/Documents/MATLAB/matlabdll/Thinkgear.dll: unknown file type, first eight bytes: 0x4D 0x5A 0x90 0x00 0x03 0x00 0x00 0x00
Error in readRAW (line 27)
loadlibrary('Thinkgear.dll');
Caused by:
Error using loaddefinedlibrary
dlopen(/Users/ahmadazhari/Documents/MATLAB/matlabdll/Thinkgear.dll, 6): no suitable image found. Did find:
/Users/ahmadazhari/Documents/MATLAB/matlabdll/Thinkgear.dll: unknown file type, first eight bytes: 0x4D 0x5A 0x90 0x00 0x03 0x00 0x00 0x00
>>
FYI: Thinkgear.dll and Thinkgear.h in the same folder with this code.
Can you help me?
Thanks for your kind :)
Geoff Hayes
2015년 2월 18일
Zaonly - please post this as a separate question as it is not related to the one posed by Rui.
preksha pareek
2019년 7월 23일
What if I dont have C file only file with extension .mexa64 is avilable?
Walter Roberson
2019년 7월 23일
You cannot use mexa64 with Mac: those are for Linux only.
There are no tools to decompile .mex* files into matlab source code.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)