将MATLAB结构返回到C++;并访问元素 我尝试从C++中运行MATLAB,并将MATLAB输出返回到一个C++结构中。该结构可以包含任意数量的内容,包括不同维度和长度的数组。有一个类似的问题,但答案没有提供足够的细节让我理解和推断

将MATLAB结构返回到C++;并访问元素 我尝试从C++中运行MATLAB,并将MATLAB输出返回到一个C++结构中。该结构可以包含任意数量的内容,包括不同维度和长度的数组。有一个类似的问题,但答案没有提供足够的细节让我理解和推断,c++,matlab,data-structures,matlab-engine,C++,Matlab,Data Structures,Matlab Engine,我正在使用MatlabEngine.hpp和MatlabDataArray.hpp运行MATLAB。我需要返回很多输出,并尝试了其他方法,但这些方法并没有达到我想要的效果。使用结构似乎是最合乎逻辑/可读的做事方式。我将尝试用下面的例子来解释我的案例,希望这些例子的写作方式对其他有类似问题的人最有用 MWE 1-返回n×n数组 MATLAB代码 这可以用C++代码运行: C++代码 C++代码 C++代码 #包括“MatlabEngine.hpp” #包括“MatlabDataArray.hpp”

我正在使用
MatlabEngine.hpp
MatlabDataArray.hpp
运行MATLAB。我需要返回很多输出,并尝试了其他方法,但这些方法并没有达到我想要的效果。使用结构似乎是最合乎逻辑/可读的做事方式。我将尝试用下面的例子来解释我的案例,希望这些例子的写作方式对其他有类似问题的人最有用

MWE 1-返回n×n数组 MATLAB代码 <>这可以用C++代码运行:

C++代码 C++代码 C++代码
#包括“MatlabEngine.hpp”
#包括“MatlabDataArray.hpp”
#包括
int main()
{
使用名称空间matlab::engine;
matlab::data::ArrayFactory;//创建matlab数据数组工厂
std::unique_ptr matlabPtr=startMATLAB();//启动MATLAB引擎
matlabPtr->eval(u“addpath(genpath('C:/Users/…your path here…'))”);//将路径添加到MATLAB函数所在的位置。
std::载体bc{10,13};
std::vector args({
工厂。创建标量(7),
CreateArray({1,2},bc.cbegin(),bc.cend())
});
matlab::data::StructArray my_matlab_struct=factory.createStructArray(matlab::data::ArrayDimensions{1,4},std::vector{'sum',prod',a_sq',b_sq});
my_matlab_struct=matlabPtr->feval(u“simple_fun3”,args);

<>上述C++代码不起作用,我不理解结构是如何定义的,即数组维数是什么。任何帮助都是值得理解的。谢谢< /P> < P>在经过大量的搜索之后,我设法解决了问题,并在C++和Matlab之间传递了结构。解决方案是基于,但是链接上的代码不起作用。缺少命名空间。因此我把解决方案放在下面。注意:假设你已经有Matlab和C++安装程序互相连接了,这可以是整个过程的另一个过程,这取决于你的MATLAB版本,如果你使用VisualStudio,你的VisualStudio版本也会。

从C++向MATLAB传递结构 这不是我最初问题的一部分,但这可能对某些人有用

#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"

#include <iostream>
#pragma comment (lib,"libmat.lib")
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libmex.lib")
#pragma comment (lib,"libeng.lib")

int main()
{
  using namespace matlab::engine;
  matlab::data::ArrayFactory factory; // Create MATLAB data array factory
  std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine
  
  matlabPtr->eval(u"addpath(genpath('Insert the path of any MATLAB functions you plan to use'))")
  
  matlab::data::StructArray my_structure = factory.createStructArray({1, 1},{"x","y","z"}); // Create a structure with one element and fields x,y,z
  my_structure[0]["x"] = factory.createArray({1, 3},{1., 2., 3.});
  my_structure[0]["y"] = factory.createArray({1, 3},{1., 4., 9.});
  my_structure[0]["z"] = factory.createArray({1, 3},{1., 8., 27.});
  
  matlabPtr->setVariable(u"Mstruct",my_structure); // pass structure to matlab workspace
  matlabPtr->eval(u"Mstruct") // show the structure exists.
  // The array can be passed to a function using feval
}

您不能执行
std::tuple
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>

int main()
{
  using namespace matlab::engine;
  matlab::data::ArrayFactory factory; // Create MATLAB data array factory

  std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine

  matlabPtr->eval(u"addpath(genpath('C:/Users/...your path here...'))"); // Add the path to where MATLAB functions are.

  std::vector<double> bc{ 10, 13};
  std::vector<matlab::data::Array> args({
    factory.CreateScalar<double>(7),
    factory.CreateArray({ 1, 2 }, bc.cbegin(), bc.cend())
  });
  
  matlab::data::TypedArray<double> results = matlabPtr->feval(u"simple_fun1",args); // Run simple_fun1
  
  std::cout << "Sum: " << results[0][0] << std::endl;
  std::cout << "Prod: " << results[0][1] << std:endl;
  std::cout << "Three: " << results[1][0] << std::endl;
}
function [sum, prod] = simple_fun2(a, bc)
% Takes input values of a and an array [a,b]
b = bc(1);
c = bc(2);
sum = a+b+c;
prod = a*b*c;
  std::tuple<double, double> results;
  results = matlabPtr->feval< std::tuple<double, double>>(u"simple_fun2", double(7), std::vector<double>{ 10, 13}); // Just as another example of how to enter inputs in case that's helpful for anyone.
  double s;
  double p;
  std::tie(s, p) = results;
  std::cout << "Sum: " << s << ", Prod: " << p << std::endl;
function output = simple_fun3(a, bc)
b = bc(1);
c = bc(2);
output.sum = a+b+c;
output.prod = a*b*c;
output.a_sq = a*a;
output.b_sq = b*b;
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>

int main()
{
  using namespace matlab::engine;
  matlab::data::ArrayFactory factory; // Create MATLAB data array factory

  std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine

  matlabPtr->eval(u"addpath(genpath('C:/Users/...your path here...'))"); // Add the path to where MATLAB functions are.

  std::vector<double> bc{ 10, 13};
  std::vector<matlab::data::Array> args({
    factory.CreateScalar<double>(7),
    factory.CreateArray({ 1, 2 }, bc.cbegin(), bc.cend())
  });
  
  matlab::data::StructArray my_matlab_struct = factory.createStructArray(matlab::data::ArrayDimensions{ 1, 4}, std::vector<std::string>{'sum', 'prod', 'a_sq', 'b_sq'});
  my_matlab_struct = matlabPtr->feval(u"simple_fun3",args);
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"

#include <iostream>
#pragma comment (lib,"libmat.lib")
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libmex.lib")
#pragma comment (lib,"libeng.lib")

int main()
{
  using namespace matlab::engine;
  matlab::data::ArrayFactory factory; // Create MATLAB data array factory
  std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine
  
  matlabPtr->eval(u"addpath(genpath('Insert the path of any MATLAB functions you plan to use'))")
  
  matlab::data::StructArray my_structure = factory.createStructArray({1, 1},{"x","y","z"}); // Create a structure with one element and fields x,y,z
  my_structure[0]["x"] = factory.createArray({1, 3},{1., 2., 3.});
  my_structure[0]["y"] = factory.createArray({1, 3},{1., 4., 9.});
  my_structure[0]["z"] = factory.createArray({1, 3},{1., 8., 27.});
  
  matlabPtr->setVariable(u"Mstruct",my_structure); // pass structure to matlab workspace
  matlabPtr->eval(u"Mstruct") // show the structure exists.
  // The array can be passed to a function using feval
}
  matlab::data::StructArray my_matlab_struct = matlabPtr->feval(u"matlab_fun",args);
  matlab::data::ArrayDimensions dims = my_matlab_struct.getDimensions();
  std::cout << "Structure is: " << dims[0] << " by " << dims[1] << std::endl;
  size_t numFields = my_matlab_struct.getNumberOfFields();
  std::cout << "Structure has " << numFields << " fields." << std::endl;

  matlab::data::Range<matlab::data::ForwardIterator, matlab::data::MATLABFieldIdentifier const> fields = my_matlab_struct.getFieldNames();
  
  for (int i=0; i<numFields; i++){
    matlab::data::TypedArray<double> data = my_matlab_struct[0][fields.begin()[i]]; // [0] for the first element of the structure. Iterates over the fields.
    matlab::data::ArrayDimensions data_dims = data.getDimensions();
    for (int j=0; j<data_dims[0];j++){
      for (int k=0; k<data_dims[1];k++){
        std::cout << data[j][k] << " ";
      }
      std::cout << std::endl;
    }
  }