What is the right way of reading MATLAB formatted data file in C++

17 次查看(过去 30 天)
I am trying to read a MATLAB formatted data file (data.mat) file in C++. Below is the sample code that am using.
using namespace std;
int main() {
MATFile *pmat;
pmat = matOpen("data.mat","r");
return 0;
}
I have saved the required header files in the code directory i.e. 'mat.h', 'matrix.h' and 'tmwtypes.h'.
Below is the output i am getting.
Command:
g++ main.cpp -o out
Output:
Undefined symbols for architecture x86_64:
"_matOpen_800", referenced from:
_main in main-d0f06c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can someone please tell me how can i fix this or what is the right way of reading a MATLAB formatted data?
  2 个评论
dpb
dpb 2020-3-15
"I have saved the required header files in the code directory i.e. 'mat.h', 'matrix.h' and 'tmwtypes.h'. "
You don't need to do that, but you've not include 'd them nor apparently set a needed compiler directive for the linker.
I know absolutely nothing about C++ but the failure is in the linker

请先登录,再进行评论。

回答(1 个)

Rashed Mohammed
Rashed Mohammed 2020-3-18
Hi Raghav,
I understand that you are having issues compiling the C++ program which reads MAT files. The recommended way of doing this is using mex command in MATLAB.
1. First open MATLAB and setup the mex.
mex -setup -client engine C++
2. Compile the C++ file using mex
mex -v -client engine filename.cpp
3. Add the MATLAB shared libraries folder to the runtime library path of your OS. You can see how to do that here
4. Open the command prompt/terminal in the folder where the executable is present and run the executable.
For more info on how to read MAT files in C/C++, please refer to the example here.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Call C++ from MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by