Using C++ Mex Function how to get std::string argument?

41 次查看(过去 30 天)
This seems like it should be simple, but I can't get either it to compile or not fail during runtime. Basically I need to have the Mex Function have 2 parameters which are both strings, and will be passed through to C++ functions inside. Can someome tell me how to go from matlab::mex::ArgumentList input, to 2 std::strings? I guess also what would function call look like from Matlab side?
Thanks,

采纳的回答

Karola Gurrath
Karola Gurrath 2019-1-30
I got the same problem yesterday and this is the solution I found. Maybe this is not the best way to get a std::string but it works and I could not find anything else...
Matlab-Code:
cellArray{1} = "string1";
cellArray{2} = "string2";
myMexFunction(cellArray);
myMexFunction:
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function
{
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
ArrayFactory factory;
public:
void operator()(ArgumentList outputs, ArgumentList inputs)
{
CellArray imageCell = std::move(inputs[0]);
TypedArrayRef<MATLABString> inArrayRef1 = imageCell[0];
std::string string1 = std::string(inArrayRef1[0]);
TypedArrayRef<MATLABString> inArrayRef2 = imageCell[1];
std::string string2 = std::string(inArrayRef2[0]);
// do something with string1 und string2...
}
}

更多回答(1 个)

cui,xingxing
cui,xingxing 2022-8-10
编辑:cui,xingxing 2024-4-27,2:28
matlab code
input1 = "string1";
input2 = "string2";
myMexFunction(input1,input2);
mex c++ code
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function
{
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
ArrayFactory factory;
public:
void operator()(ArgumentList outputs, ArgumentList inputs)
{
std::string string1 = std::string(inputs[0][0]);
std::string string2 = std::string(inputs[1][0]);
// or use following code
//std::string string1 =
// matlab::engine::convertUTF16StringToUTF8String(inputs[0][0]);
//std::string string2 =
// matlab::engine::convertUTF16StringToUTF8String(inputs[1][0]);
// do something with string1 und string2...
}
}
input1/2 argument is one string scalar in matlab, but it also represent 1 by 1 string matrix, so
directly specify sub-index [0][0] in c++.
BTW, to prevent matlab crash from incorrect input, It is best to have a check-uphad
if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING) {
matlabPtr->feval(u"error",
0, std::vector<matlab::data::Array>({factory.createScalar("The first input must string scalar")}));
}
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com

类别

Help CenterFile Exchange 中查找有关 Write C++ Functions Callable from MATLAB (MEX Files) 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by