Main Content

matlab::engine::StreamBuffer

Define stream buffer

Description

Type definition for std::basic_streambuf<char16_t>.

Examples

This example defines string buffers to return output from the evaluation of a MATLAB® function by the MATLABEngine::eval member function. This function uses a buffer derived from matlab::engine::StreamBuffer to return output from MATLAB to C++.

#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>

using namespace matlab::engine;
using SBuf = std::basic_stringbuf<char16_t>;

void printFromBuf(const std::shared_ptr<SBuf> buf)
{
    //Get text from buf
    auto text_ = buf->str();
    std::cout << "*" << convertUTF16StringToUTF8String(text_)
        << "*" << std::endl;
}

int main() {

    //Create Array factory 
    matlab::data::ArrayFactory factory;

    // Connect to named shared MATLAB session started as:
    // matlab -r "matlab.engine.shareEngine('myMatlabEngine')"
    String session(u"myMatlabEngine");
    std::unique_ptr<MATLABEngine> matlabPtr = connectMATLAB(session);

    auto outBuf = std::make_shared<SBuf>();
    auto errBuf = std::make_shared<SBuf>();

    matlabPtr->eval(u"matlab.engine.engineName", outBuf, errBuf);
    printFromBuf(outBuf);
    printFromBuf(errBuf);
    return 0;
}
Introduced in R2017b