I have a 2-D double-precision array in MATLAB that contains specific data.
I want to use this array in c++, so I save the array in a mat-file.
I know that MATLAB has some c functions that provide reading mat-file in c++ (matdsgn , matOpen , ...), but I don't know how to use them in a c++ program. Actually, I don't know how to use a C library in C++.
Any help would be appreciated.
If you have MATLAB 2017a, there is a built-in function. See this MATHWORKS link: Math Works
Related
Bigger context: I'm trying to use CoreMotion's CMRotationMatrix in C++ code. I'm using a wrapper to bring Swift code into C++.
However, I don't know how to bring the CMRotationMatrix into my C++ opencv code. With this question (How to retrieve value from C++ matrix in Swift?) I get the sense that I'm supposed to convert the CMRotationMatrix into uint8_t array of arrays but I'm not sure if this is the right path and even if it is, I don't know how to convert uint8_t to cv::Mat.
Any help would be appreciated. Thanks!
At the moment, I call a C++ function from a dynamic library in Mathematica with given arguments/parameters. These include strings, integers, sparse matrices etc.
output = dllfunction[string, int, int, SparseMatrix, outputLocation,...];
Within the dynamic library, the arguments are loaded using <WolframLibrary.h>. Now I want to separate the Mathematica part from the C++ part.
How do I save the Mathematica parameters so I can load them in a separate C++ executable using <WolframLibrary.h>?
I write code based on modern Fortran. For some reason, I want to modify it in a way that is compatible with the old version. Converting from the latest version to version 95 is desirable here. I have trouble with two intrinsic functions. "Mov_alloc" and "Norm2" are parts of these functions.
I want to know: are there any intrinsic functions for them in Fortran 95? Or, are there any external functions that do the same job precisely?
You can easily implement norm2() yourself based on the definition. Some care must be taken if your numbers are so large that overflow is an issue. But the simplest version is as simple
norm2 = sum(A**2)
There is no equivalent of move_alloc() in Fortran 95. You may need to use pointers instead of allocatable variables. You could implement your own version in C, but that would require many features from Fortran 2003-2018, so it makes little sense for you.
You can consider reallocating your arrays yourself and copying the data instead of doing move_alloc():
if (allocated(B)) deallocate(B)
allocate(B(lbound(A,1):ubound(A,1)))
B(:) = A
deallocate(A)
However, it is not the same as move_alloc().
For the now being I am developing a C++ program based on some MATLAB codes. During the developing period I need to output the intermediate results to MATLAB in order to compare the C++ implementation result with the MATLAB result. What I am doing now is to write a binary file with C++, and then load the binary file with MATLAB. The following codes show an example:
int main ()
{
ofstream abcdef;
abcdef.open("C:/test.bin",ios::out | ios::trunc | ios::binary);
for (int i=0; i<10; i++)
{
float x_cord;
x_cord = i*1.38;
float y_cord;
y_cord = i*10;
abcdef<<x_cord<<" "<<y_cord<<endl;
}
abcdef.close();
return 0;
}
When I have the file test.bin, I can load the file automatically with MATLAB command:
data = load('test.bin');
This method can work well when numerical data is the output; however, it could fail if the output is a class with many member variables. I was wondering whether there are better ways to do the job not only for simple numerical data but also for complicated data structure. Thanks!
I would suggest the use of MATLAB engine through which you can pass data to MATLAB on real time basis and can even visualize the data using various graph plotting facilities available in MATLAB.
All you have to do is to invoke the MATLAB engine from C/C++ program and then you can easily execute MATLAB commands directly from the C/C++ program and/or exchange data between MATLAB and C/C++. It can be done in both directions i.e. from C++ to MATLAB and vice versa.
You can have a look at a working example for the same as shown here.
I would suggest using the fread command in matlab. I do this all the time for exchanging data between matlab and other programs, for instance:
fd = fopen('datafile.bin','r');
a = fread(fd,3,'*uint32');
b = fread(fd,1,'float32');
With fread you have all the flexibility to read any type of data. By placing a * in the name, as above, you also say that you want to store into that data type instead of the default matlab data type. So the first one reads in 3 32 bit unsigned integers and stores them as integers. The second one reads in a single precision floating point number, but stores it as the default double precision.
You need to control the way that data is written in your c++ code, but that is inevitable. You can make a class method in c++ that packs the data in a deterministic way.
Dustin
I have a very large data structure in some Matlab code that is in the form of cells of arrays. We want to develop C code to work on this data, but I need some way to store the Matlab variable (which we generate in Matlab) and open it in a C/C++ program. What is the easiest way to bridge the two programs so I can transfer the data?
If you are only moving the data from MATLAB to C occassionally, the easiest thing would be to write it to a binary file, then read from the file in C. This of course leaves the C code completely independent of MATLAB.
This does not have to be that messy if your data structure is just a cell array of regular arrays, e.g.
a{1} = zeros(1,5);
a{2} = zeros(1,4);
You could just write a header for each cell, followed by the data to the file. In the above case, that would be:
[length{1} data{1} length{2} data{2}]
In the above case:
5 0 0 0 0 0 4 0 0 0 0
If the arrays are 2D, you can extend this by writing: row, column, then the data in row-major order for each cell.
This might not be entirely convenient, but it should be simple enough. You could also save it as a .mat file and read that, but I would not recommend that. It is much easier to put it in a binary format in MATLAB.
If you need to move the data more frequently than is convenient for a file, there are other options, but all I can think of are tied to MATLAB in some way.
You should use mex files:
http://www.mathworks.fr/support/tech-notes/1600/1605.html
If the two processes need to connect during their lifecycle, you have plenty of options:
Compile Matlab DLL.
Use Matlab Engine.
Compile MEX file (as #Oli mentioned earlier)
If the communication is offline (After Matlab closes, C++ starts to read), then you should use filesystem. Try to format it in XML, it is a well recognized standard.