How to create matlab::da​ta::CharAr​rayRef for matlab::data::CharArray?

조회 수: 3 (최근 30일)
I'm using the C++ (not C!) MATLAB Data API from MATLAB R2019b. I would like to handle data referenced by a matlab::data::CharArray and a matlab::data::CharArrayRef in the same manner, i.e. I would like to implement something such as the following in C++ without using code duplication:
std::string toString(matlab::data::CharArray const& in) {
// TODO(2021-12-13 by wolters): How-to create `CharArrayRef` from
// `CharArray` in order to call the other toString function and avoid
// code duplication?
auto const multiByte = in.toUTF16();
return std::string{multiByte.begin(), multiByte.end()};
}
std::string toString(matlab::data::CharArrayRef const& in) {
auto const multiByte = in.toUTF16();
return std::string{multiByte.begin(), multiByte.end()};
}
I've tried a lot, but can't find a way to create an instance of matlab::data::CharArrayRef from an existing matlab::data::CharArray. (the same applies to other data types of the MATLAB Data API). Do you know how to achieve that?

채택된 답변

Eswaramoorthy
Eswaramoorthy 2023년 2월 28일
Hi
Yes, you can create an instance of matlab::data::CharArrayRef from an existing matlab::data::CharArray by using the createArrayRef function. Here's an example of how you could modify your code to avoid code duplication:
std::string toString(matlab::data::CharArray const& in) {
auto const multiByte = in.toUTF16();
return std::string{multiByte.begin(), multiByte.end()};
}
std::string toString(matlab::data::CharArrayRef const& in) {
auto const multiByte = in.toUTF16();
return std::string{multiByte.begin(), multiByte.end()};
}
// Wrapper function that calls the appropriate toString function
std::string toStringWrapper(const matlab::data::CharArray& in) {
return toString(in.createArrayRef());
}
In the toStringWrapper function, we create a matlab::data::CharArrayRef object from the matlab::data::CharArray input using the createArrayRef function. This matlab::data::CharArrayRef object can then be passed to the toString function that takes a matlab::data::CharArrayRef input. By using this wrapper function, you can avoid code duplication while still being able to handle both matlab::data::CharArray and matlab::data::CharArrayRef inputs in the same manner

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by