2

I have a program that processes 2D array of doubles at several stages. I want to compare its results with MATLAB. I can perhaps use std::cout to print some of 8x8 size blocks the 2D array before and after processing (the algorithm works on blocks) onto the console and then manually type those numbers into MATLAB. But that is error prone and tedius. Is there a way to conviniently get this data into MATLAB?

Matlab has built in functions to simplify many things. I want to get the data before and after processing in the C++ program into MATLAB and then run some checks on it e.g draw graphs and stuff. How do I get the data from the C++ program into MATLAB?

quantum231
  • 2,420
  • 3
  • 31
  • 53
  • 5
    Just redirect the output of your C++ program to a text file and then [load this directly into MATLAB](https://uk.mathworks.com/help/matlab/ref/importdata.html). – Paul R May 10 '17 at 22:50
  • 1
    A text file and not binary file? OK – quantum231 May 10 '17 at 22:55
  • 1
    I see, I can import the data and then split into blocks in a 3D array. That is nice. In my understanding MATLAB could only input a .m file which I am not sure how to create in C++ – quantum231 May 10 '17 at 23:00
  • 1
    Text format is generally preferable unless you have a huge amount of data such that file size becomes a problem - text files are much easier to deal with and more portable. – Paul R May 10 '17 at 23:02
  • .m files are for MATLAB program code - however MATLAB supports a wide range of file formats for data I/O - various text formats as well as binary and various image formats. – Paul R May 10 '17 at 23:03
  • Yes... the flat file approach is the best here, more than any other esoteric data transfer you could think on implement......... – Brethlosze May 11 '17 at 02:29
  • @PaulR Is there an issue with decimal representation compared to the internal binary/float representations, or are the differences generally considered too small to worry about? – Steve May 11 '17 at 08:17
  • 2
    @Steve: in general it's feasible to read/write binary float values, but there can be compatibility issues with e.g. 8/10 byte doubles, and even endianness in some cases. Text is just much easier to deal with, since it's human-readable, and can be read/written by many different tools, e.g. spreadsheets, databases, etc. – Paul R May 11 '17 at 13:59

3 Answers3

2

Have you considered using mex functions? If you do use them, I highly recommend using the Armadillo library, which provides convenient data types and methods to switch between MATLAB and C++.

For example,

#include "mex.h"
#include <armadillo>
#include "armaMex.hpp"
#include <sstream>    

// gateway function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{     
// get data from MATLAB
mat X = conv_to<mat>::from(armaGetPr(prhs[0],true));
mat Y = conv_to<mat>::from(armaGetPr(prhs[1],true));

// do some stuff
mat Z = X + Y;

// Print data in matlab
std::ostringstream buffer;
buffer << "X = " << X << endl;
buffer << "Y = " << Y << endl;
buffer << "Z = " << Z << endl;
mexPrintf("%s", buffer.str().c_str());    

// send data back to Matlab
plhs[0] = armaCreateMxMatrix(Z.n_rows, Z.n_cols, mxDOUBLE_CLASS, mxREAL); 
armaSetPr(plhs[0], conv_to<mat>::from(Z));

return;
}
kedarps
  • 851
  • 9
  • 19
1

It's very easy to get numerical data from a C++ program to Matlab. Just output numbers as ascii text to a file then read it in using load Separate each number in a column by a space and each row by a newline.

Let's take a 3 rows of 2 numbers as an example and say you create a file "text.txt" that looks like this:

1 2
3 4
5 6

Then, in Matlab, the command:

 load text.txt

will read the data into a variable named text that is a 3x2 matrix.

doug
  • 3,840
  • 1
  • 14
  • 18
  • I thought matlab only works with .m files and some other limited non c++ friendly formats. But now it is clear that is not the case. – quantum231 May 11 '17 at 20:41
1

In addition, you could do a little bit of pretty outputting and directly write to a runnable .m file (I use this approach to get numerics into LaTeX documents). A benefit of this approach is that you could define multiple MATLAB variables with a single file. For example:

#include <fstream>

int main() {
  std::ofstream file;
  file.open ("cpp_output.m");
  file << "out = [\n";
  for (double x = 1; x<6; x++) {
    file << x << ' ' << 1/(x*x) << '\n';
  }
  file << "];";
  file.close();
}

produces the file cpp_output.m

out = [
1 1
2 0.25
3 0.111111
4 0.0625
5 0.04
];

You probably want to change the precision (e.g. How do I print a double value with full precision using cout?)

Community
  • 1
  • 1
Steve
  • 1,579
  • 10
  • 23