通过从c++变量MATLAB
方法通过变量
你可以通过MATLAB c++变量®使用这些技巧:
通过调用的变量作为函数参数
matlab:引擎::MATLABEngine
函数宏指令或fevalAsync成员函数。变量作为参数传递给函数调用不存储在MATLAB基础工作空间。有关更多信息,请参见从c++调用MATLAB函数。把MATLAB基础变量或全局工作空间使用
matlab:引擎::MATLABEngine
setVariable和setVariableAsync成员函数。在MATLAB中使用全局变量的更多信息,参见MATLAB全球
函数。
您可以创建变量在MATLAB工作区中使用matlab:引擎::MATLABEngine
eval和evalAsync成员函数。使用这些函数来执行MATLAB语句,使作业变量。有关更多信息,请参见评估从c++ MATLAB语句。
把变量MATLAB基本工作空间
这个示例代码执行以下步骤:
将MATLAB的工作区中使用的变量
MATLABEngine: setVariable
使用这些变量来调用MATLAB
movsum
函数使用MATLABEngine: eval
得到了输出变量
一个
从MATLAB工作区使用MATLABEngine: getVariable
。
这是等价的MATLAB代码。
一个= movsum ([4 8 6 1 2 3 1 3 4 5], 3,“端点”,“丢弃”);
这是c++代码。
# include“MatlabDataArray。hpp MatlabEngine # include。高压泵”# include < iostream >
空白callputVariables(){使用名称空间matlab::引擎;/ /开始MATLAB引擎同步std:: unique_ptr < MATLABEngine > matlabPtr = startMATLAB ();/ /创建MATLAB数据数组工厂MATLAB: data:: ArrayFactory工厂;/ /创建变量matlab::数据::TypedArray <双> data =工厂。createArray <二> (10 {1,},{4、8、6、1、2、3、1,3,4,5});matlab::数据::TypedArray < int32_t > windowLength = factory.createScalar < int32_t > (3);matlab::数据::CharArray name = factory.createCharArray(“端点”);matlab::数据::CharArray value = factory.createCharArray(“丢弃”);/ /把变量放在MATLAB工作区matlabPtr - > setVariable (u“数据”,std::移动(数据));matlabPtr - > setVariable (u“w”, std::移动(windowLength)); matlabPtr->setVariable(u"n", std::move(name)); matlabPtr->setVariable(u"v", std::move(value)); // Call the MATLAB movsum function matlabPtr->eval(u"A = movsum(data, w, n, v);"); // Get the result matlab::data::TypedArrayconst A = matlabPtr->getVariable(u"A"); // Display the result int i = 0; for (auto r : A) { std::cout << "results[" << i << "] = " << r << std::endl; ++i; } }
如何设置和信息构建c++引擎项目,明白了构建c++引擎项目。
另请参阅
matlab:引擎::MATLABEngine
|matlab:引擎::startMATLAB