Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a 2-D double-precision array in MATLAB that contains specific data. I want to open and use this array in my c++ program, in the first step I save it in a mat-file. 


What I have tried:

I know that MATLAB has some c functions that provide reading mat-file in c++ (matdsgn , matOpen , ...), but I have no idea how to use these functions in a c++ program. Actually, I don't know how to use a C library in C++. Any help would be appreciated.
Posted
Updated 28-Aug-17 2:46am

C library functions can be called directly from a C++ program. For example:
C++
#include <math.h>

double foo(double value, int power)
{
    double answer = pow(value, (double)power);

// other code gor this method

    return answer;
}


The matlab documentation is the place to look for conversion functions.
 
Share this answer
 
To use a C library with C++ just link your application with the library and include the library header file(s) in your source or header files that call functions from the library or use types defined in the library header files.

According to MAT-File Library and Include Files - MATLAB & Simulink - MathWorks United Kingdom[^] there are only shared library versions. So you have to distribute them with your application.
 
Share this answer
 
Comments
fatemeh rastegar 28-Aug-17 9:08am    
thanks jochen for your answer, there are "include files" and "library files" for reading mat-files in the page you mentioned.I work with visual studio 2015.I created a c++ project and add those files to my project (plus the matdsgn.c file that reads the mat-file) and build my project.I've got these errors:


1>matdgns.obj : error LNK2019: unresolved external symbol _mxFree referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _mxGetNumberOfDimensions_730 referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _mxIsFromGlobalWS referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _mxDestroyArray referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _matOpen referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _matClose referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _matGetNextVariable referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _matGetNextVariableInfo referenced in function _diagnose
1>matdgns.obj : error LNK2019: unresolved external symbol _matGetDir referenced in function _diagnose
1>C:\Program Files\MATLAB\R2014a\bin\win64\gsdll32.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86'
1>C:\Program Files\MATLAB\R2014a\bin\win64\libiomp5md.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'X86'
1>E:\My Data\AUT\M.Sc\AMIRLab\M.Sc Project\My codes in c++\mat_in_c\Debug\mat_in_c.exe : fatal error LNK1120: 9 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Jochen Arndt 28-Aug-17 9:22am    
"library machine type 'x64' conflicts with target machine type 'X86'"

You have to link with the 32-bit version of the library or build a 64-bit application. I'm not abosolutely sure if the unresolved external errors are sourced by this mismatch but I guess it is so.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900