what's the difference between these matlab data API strings functions?

6 Ansichten (letzte 30 Tage)
" matlab::data::MATLABString" ,"matlab::data::String", "matlab::engine::String","factory.createArray<MATLABString>" ... have some some similarities,these were introduced in R2017b onwards, do they have some applicable scenarios? How should these be convert to std::string each other, which is said to be (UTF-16 string) in matlab and (UTF-8 string) in std::string in c++?
I know matlab::engine::String convert to std::string each other by "convertUTF8StringToUTF16String" or "convertUTF16StringToUTF8String",but " matlab::data::MATLABString" ,"matlab::data::String" how to convert to std::string each other?

Akzeptierte Antwort

Madheswaran
Madheswaran am 29 Mai 2025
Hi cui xingxing,
I will address your questions one by one.
...these were introduced in R2017b onwards, do they have some applicable scenarios?
  • matlab::data::String is the basic UTF-16 string type (std::basic_string<char16_t>) defned within the matlab::data namespace.
  • matlab::data::MATLABString is an 'optional object' wrapped around matlab::data::String, allowing you to represent missing string values (For more information on 'optional' refer here: https://mathworks.com/help/matlab/apiref/matlab.data.optional.html). Use this when your string data might contain missing values that need to be preserved.
  • matlab::engine::String is also a UTF-16 string type (std::basic_string<char16_t>) but belongs to the matlab::engine namespace. Use this when working with MATLAB engine functions, such as connecting to MATLAB sessions.
  • factory.createArray<MATLABString>() is a factory method from matlab::data::ArrayFactory that creates arrays containing MATLABString elements. Use this when you need to create string arrays that can contain missing values.
...but "matlab::data::MATLABString" ,"matlab::data::String" how to convert to std::string each other?
Converting matlab::data::String to std::string:
matlab::data::String str_ml = get_some_string(); // your string here
std::string str(str_ml.begin(), str_ml.end());
Converting matlab::data::MATLABString to std::string:
matlab::data::MATLABString str_optional = get_some_string(); // your string here
if (str_optional.has_value()) {
matlab::data::String str_ml = str_optional;
std::string str(str_ml.begin(), str_ml.end());
} else {
// Handle missing value case
}
Important Note: The simple conversion method shown above only works correctly for ASCII characters. For proper UTF-16 to UTF-8 conversion with Unicode support, you would need additional conversion logic.
For more information, refer to the followng documentations:
  1. https://mathworks.com/help/matlab/apiref/matlab.data.string.html
  2. https://mathworks.com/help/matlab/apiref/matlab.data.matlabstring.html
  3. https://mathworks.com/help/matlab/apiref/matlab.data.optional.html
Hope this helps!

Weitere Antworten (0)

Kategorien

Mehr zu Call C++ from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by