通过变量MATLABC + +
通过MATLAB的变量®基地或全球工作区c++程序使用matlab:引擎::MATLABEngine
getVariable和getVariableAsync成员函数。返回变量的c++作为matlab::数据::数组
。
如何设置和信息构建c++引擎项目,明白了要求构建c++引擎项目。
将MATLAB计算到c++的结果
这个示例代码执行了在MATLAB计算使用MATLABEngine: eval
并得到结果MATLABEngine: getVariable
。
MATLABcart2sph
在笛卡尔坐标转换为在球坐标表示。
# include“MatlabDataArray。hpp MatlabEngine # include。高压泵”# include < iostream >
空白callgetVars(){使用名称空间matlab::引擎;/ /开始MATLAB引擎同步std:: unique_ptr < MATLABEngine > matlabPtr = startMATLAB ();/ /评估MATLAB语句matlabPtr - > eval (u”(az, el, r) = cart2sph (5、7、3);”);/ /从MATLAB MATLAB得到结果:data:: TypedArray <双> result1 = matlabPtr - >编写此表达式getVariable (u“az”);matlab::数据::TypedArray <二> result2 = matlabPtr - > getVariable (u“el”);matlab::数据::TypedArray <二> result3 = matlabPtr - > getVariable (u r);/ /显示结果std:: cout < <“阿兹:“< < result1[0] < <编写此表达式std:: endl;std:: cout < <”埃尔:“< < result2 [0] < < std:: endl;std:: cout < <“r:“< < result3 [0] < < std:: endl;}
得到MATLAB对象和访问属性
使用matlab:引擎::MATLABEngine
getVariable或getVariableAsync成员函数MATLAB对象变量。c++是一个返回对象matlab::数据::数组
。访问对象属性使用matlab:引擎::MATLABEngine
getProperty或getPropertyAsync成员函数。
此示例代码创建了一个MATLAB图
c++对象并返回对象句柄。值的图单位
房地产总是字符数组。因此,查询图单位
属性,返回一个matlab:: data:: CharArray
与图的值属性(默认值是多少像素
)。
# include“MatlabDataArray。hpp MatlabEngine # include。高压泵”# include < iostream >
空白callgetVariables(){使用名称空间matlab::引擎;/ /开始MATLAB引擎同步std:: unique_ptr < MATLABEngine > matlabPtr = startMATLAB ();/ /创建图窗口matlabPtr - > eval (u“figureHandle =图;”);/ /得到图处理和单位财产matlab:数据::数组figHandle = matlabPtr - > getVariable (u“figureHandle”);matlab::数据::CharArray单位= matlabPtr - > getProperty (figHandle u“单位”);/ /显示属性值std:: cout < <“单位性质:“< < units.toAscii () < < std:: endl;}
从对象数组得到财产
如果对象变量是一个对象数组,调用getProperty
或getPropertyAsync
数组中的索引的对象,你想访问。例如,要获取的值单位
属性的第四个元素对象数组,objectArray
作为第二个输入参数,指定索引。
matlab::数据::CharArray单位= matlabPtr - > getProperty (objectArray 3 u“单位”);
设置属性MATLAB对象
将MATLAB的价值从c++对象属性,使用matlab:引擎::MATLABEngine
setProperty或setPropertyAsync成员函数。访问对象属性,您可以得到对象变量从MATLAB工作区和c++变量设置值。设置更新对象的属性值在MATLAB工作区。
你也可以从调用返回一个对象MATLABEngine:函数宏指令
和MATLABEngine: fevalAsync
和设置属性值的对象。
此示例代码创建了一个MATLAB图
c++对象并返回对象。代码设置图颜色
财产红色的
的值,从而改变MATLAB工作区对象上的属性。
因为的价值颜色
属性是一个char数组,使用matlab:: data:: ArrayFactory
创建一个matlab:: data:: CharArray
定义新值。
空白getObject(){使用名称空间matlab::引擎;/ /开始MATLAB引擎同步std:: unique_ptr < MATLABEngine > matlabPtr = startMATLAB ();/ /创建MATLAB数据数组工厂MATLAB: data:: ArrayFactory工厂;/ /创建图窗口size_t numArguments (1);std::向量< matlab::数据::数组> figureHandle = matlabPtr - >函数宏指令(u“图”,numArguments, {});/ /暂停显示图matlabPtr - > eval (u“暂停(5)”);/ /设置颜色属性为红色matlabPtr - > setProperty (figureHandle [0], u“颜色”,factory.createCharArray(“红”));matlabPtr - > eval (u“暂停(10)”);}
设置属性的对象数组
如果对象变量是一个对象数组,调用setProperty
或setPropertyAsync
数组中的索引的对象,你想访问。例如,设置的值颜色
属性的第四个元素对象数组,objectArray
作为第二个输入参数,指定索引。
matlabPtr - > setProperty (objectArray 3 u“颜色”,factory.createCharArray(“红”));
另请参阅
matlab:引擎::MATLABEngine
|matlab:引擎::startMATLAB