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

5 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 15 Mar 2020
"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

Accedi per commentare.

Risposte (1)

Rashed Mohammed
Rashed Mohammed il 18 Mar 2020
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!

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by