필터 지우기
필터 지우기

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

조회 수: 49 (최근 30일)
Brian
Brian 2019년 1월 29일
편집: cui,xingxing 2024년 4월 27일
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일
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