How to use mexPrintf with C++ MEX Functions

Hi there
I am testing the new C++ MEX API with Matlab 2018b. However, when try to use mexPrintf, I found that it is only defined in mex.h but not mex.hpp. Include mex.hpp and mex.h in the same time is not allowed in mex function. Is there any other ways to use mexPrinft or print out something in matlab command windows if C++ MEX API is used?
A simple example for testing is attached below. If #include <mex.h> is uncommented, compilation will fail.
#include <mex.hpp>
#include <mexAdapter.hpp>
//#include <mex.h>
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function
{
public:
void operator()(ArgumentList outputs, ArgumentList inputs)
{
const double offSet = inputs[0][0];
TypedArray<double> doubleArray = std::move(inputs[1]);
for (auto& elem : doubleArray)
{
elem += 1*offSet;
}
outputs[0] = doubleArray;
//mexPrintf("Hello");
}
};

 채택된 답변

Steven Lord
Steven Lord 2019년 1월 3일

0 개 추천

Take a look at this documentation page for one potential technique.

댓글 수: 4

wh z
wh z 2019년 1월 3일
It works! Thanks~
Tucker Downs
Tucker Downs 2021년 10월 9일
This is a terrible solution. It shouldn't take so much code (from our, the users, side) to run disp "hello world" from mex. Why isn't there a convienience function in the C++ api?
Cris Luengo
Cris Luengo 2021년 12월 6일
This is indeed ridiculous. The C++ API could very easily define an output stream that is connected to the MATLAB console, something like matlab::engine::cout.
Jonathan
Jonathan 2023년 4월 20일
Still not fixed yet. Put in ticket request:
"Thank you for using our products and providing helpful feedback. An enhancement request has been submitted and we will let you know when we finalize the update."

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

질문:

2019년 1월 3일

댓글:

2023년 4월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by