Display C++ Output in Matlab using Mexfunctions

14 ビュー (過去 30 日間)
Hadi Ibrahim
Hadi Ibrahim 2022 年 10 月 4 日
回答済み: Harshit Gupta 2022 年 10 月 7 日
Hello everybody,
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 mex function calls other classes where my output is generated ... to keep the matter simple and clear, here is an example ...
i have the following mex function:
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
does anyone have an idea how i can display the string "Hello world" in matlab command window ?
Thank you in advance for your help :)
best regards

回答 (1 件)

Harshit Gupta
Harshit Gupta 2022 年 10 月 7 日
As per my understanding, you want to display C++ Output in MATLAB using Mex Functions.
MEX functions can display output in the MATLAB command window. However, some compilers do not support the use of “std::cout” in MEX functions. Another approach is to use “std::ostringstream” and the MATLAB “fprintf” Function to display text in the MATLAB command window.
Please refer to the following documentation link for an example of how to display output in the MATLAB Command Window: https://www.mathworks.com/help/matlab/matlab_external/displaying-output-in-matlab-command-window.html

カテゴリ

Help Center および File ExchangeMATLAB Code Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by