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

1 次查看(过去 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 中查找有关 C Shared Library Integration 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by