0

I have a question about display C++ output in MATLAB. I wrote a mex function to include my C++ algorithm in MATLAB but I have a problem with displaying my output in MATLAB. The c++ mex function calls other classes where my output is generated. To keep the matter simple and clear, here is an example.

Mexfunction.cpp:

#include "mex.hpp"
#include "mexAdapter.hpp" 
#include "Helper.h"
class MexFunction : public matlab::mex::Function {
public:
    void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
        // Function implementation
        ...
            Helper();
        }
};

(Helper Class, that is called in Mexfunction.cpp)

#ifndef HELPER_H
#define HELPER_H
#include <iostream>
class Helper
{
public:
    Helper()
    {
        std::cout << "Hello World" << std::endl; 
    }
};
#endif

How I can display the string "Hello world" in the MATLAB command window?

  • What version of MATLAB are you on? I was under the impression that this worked out of the box now, but maybe it still doesn't? I've linked an older Q&A that shows how to get `std::cout` to work within MEX-files. I like my own answer there the best (obvously!): https://stackoverflow.com/a/41276477/7328782 – Cris Luengo Oct 04 '22 at 15:07
  • I am using external C++ libraries with the Mexfuntion and would like to continue working with C++ mex functions. Matlab says that you can't mix MATLAB Data API functions with C Engine API functions. So I can't include mex.h at all because that is for C mex applications. Would it make a difference if I write mexfuntion (c based) that calls C++ code ? – Hadi_IronSide Oct 06 '22 at 09:58
  • You can’t use the C data API functions with data managed by the C++ data API, and vice versa. But that doesn’t mean you can’t call `mexPrintf` or include `mex.h`. — But yes, you can use the C data API in C++ code, you can build a C++ MEX-file that uses the C data API. That’s what we all did before the C++ data API was introduced in 2017 or whenever that was. – Cris Luengo Oct 06 '22 at 13:21

1 Answers1

1

For a C-MEX file, you need to use the mexPrintf function, in other words

#include "mex.h" // for mexPrintf

class Helper
{
public:
    Helper()
    {
        mexPrintf("Hello World\n"); 
    }
};

In a C++ MEX file, I just tried the following on both Linux and Windows using R2022a. The direct call to std::cout works OK, but if you're on a different thread, things do not work - could that be the problem?

#include "mex.hpp"
#include "mexAdapter.hpp"

#include <iostream>
#include <thread>

class MexFunction : public matlab::mex::Function {
public:
    void operator()(matlab::mex::ArgumentList, matlab::mex::ArgumentList) {
        std::cout << "Hello, world!\n";
        std::thread t {[]() { std::cout << "This doesn't work.\n"; }};
        t.detach();
    }
};
Edric
  • 23,676
  • 2
  • 38
  • 40
  • I thought MATLAB was correctly handling the output buffer nowadays, but maybe that was in a dream. :) I suggest mex.hpp include [this simple code](https://stackoverflow.com/a/41276477/7328782) to allow MEX-files to use `std::cout` normally. – Cris Luengo Oct 04 '22 at 15:12
  • Hm, I must admit I'm not actually sure. I thought it should work too - I think it does under certain circumstances. (Certainly once you get away from the MATLAB interpreter thread, all bets are off) – Edric Oct 04 '22 at 16:31
  • Unfortunately, Matlab says that you cannot mix MATLAB Data API functions with C Engine API functions. so I can't include mex.h at all because that's for C-mex applications :( – Hadi_IronSide Oct 06 '22 at 09:02