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

조회 수: 7 (최근 30일)
Raghav Rathi
Raghav Rathi 2020년 3월 15일
답변: Rashed Mohammed 2020년 3월 18일
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
Raghav Rathi
Raghav Rathi 2020년 3월 15일
I have included the header file in the code.

댓글을 달려면 로그인하십시오.

답변 (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에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by