Main Content

matlab::engine::WorkspaceType

Type of MATLAB workspace

Description

The matlab::engine::WorkspaceType enum class specifies the MATLAB® workspace to pass variables to or get variables from.

BASEVariables scoped to the MATLAB base workspace (command line and nonfunction scripts)
GLOBALVariables scoped to the MATLAB global workspace (command line, functions, and scripts)

MATLAB scopes variables by workspace. Variables that are scoped to the base workspace must be passed to functions as arguments. Variables scoped to the global workspace can be accessed by any function that defines the specific variable name as global.

Class Details

Namespace:

matlab::engine
IncludeMatlabEngine.hpp

Examples

This example:

  • Connects to a shared MATLAB session

  • Creates a matlab::data::Array containing numeric values of type double

  • Puts the array in the MATLAB global workspace

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

static void putGlobalVar() {

    using namespace matlab::engine;

    // 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);

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

    // Create data variable
    matlab::data::Array data = factory.createArray<double>
        ({ 1, 5 }, { 4.0, 11.0, 4.7, 36.2, 72.3 });

    // Put data variable in MATLAB global workspace
    matlabPtr->setVariable(u"data", data, WorkspaceType::GLOBAL);
}

Version History

Introduced in R2017b