string member in c++ interface

8 visualizaciones (últimos 30 días)
Jonas Steiner
Jonas Steiner el 22 de Oct. de 2021
Respondida: Shubham el 22 de Feb. de 2024
I want to use the matlab c++ interface using clibgen.generateLibraryDefinition(...) etc. bacuse i am using an api dll from an external source (i can not cahnge the source code).
In there is a struct with a string member name (concisely a wchar_t array : "wchar_t name[120]") with constant length 120 (240 Bytes).
The resulting interface does list the class but without the string member name.
I tried manually adding a member of type "string" to the class definition in the .m and the .mlx with the resulting console output:
"The property 'name' that is being added does not exist in the class."
The other problem is the fixed length in memory. It needs to be 240 Bytes and stay 240 Bytes at all times.

Respuestas (1)

Shubham
Shubham el 22 de Feb. de 2024
Hi Jonas,
When using MATLAB's C++ interface with clibgen.generateLibraryDefinition(...), there can be issues with handling certain data types, especially when dealing with strings and fixed-size character arrays like wchar_t name[120]. MATLAB's interface generation can sometimes omit these members if it doesn't recognize them or can't handle them properly.
Here's a step-by-step guide on how to address the issue:
  1. Use clibgen.generateLibraryDefinition(...) to generate the interface for your C++ library. This will create a definition file (.def) and a MATLAB class that wraps the C++ class.
  2. Open the generated definition file (.def) and look for the class that should contain the name member. You might need to manually add the member to the definition. Since MATLAB might not directly support fixed-length wchar_t arrays, you might need to represent the data in a different way that MATLAB can understand, such as using a numeric array.
  3. After manually editing the definition file, use clibgen.buildInterface(...) to generate the MATLAB interface files (.m, .mlx, and possibly .cpp wrapper files).
  4. Since MATLAB does not natively support fixed-length strings as in C++, you might need to handle the conversion manually. One way to ensure the fixed length of 240 bytes is to use a numeric array of the appropriate size and type, and then handle the conversion between the numeric array and a string within MATLAB

Categorías

Más información sobre Build MATLAB Interface to C++ Library en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by