How to load a String Array from a .mat file using C or C++ API

9 visualizaciones (últimos 30 días)
Maciej Panek
Maciej Panek el 24 de Oct. de 2019
Comentada: James Tursa el 24 de Oct. de 2019
Hello,
I have question and after a rather heavy research, I cannot find an answer to my question.
The question is:
Let's say that we declare a structure with String Array variable like this:
Header.Name = "Some string";
Then we save the workspace as a .mat file.
How can one load such variable using C or C++ API?
Using C API:
You can load "Header" mxArray
You can get a pointer to ".Name" mxAarray.
There is no way to get value of the ".Name". I cannot do that even in a MEX file. There are some solutions of getting value of private member "data" from the string class, but they no loner work in newer MATLAB's.
Using C++ API(Data API):
I am able to get value of the variable when writing a MEX using Data API. Some casts using MATLABString an I get it.
But I want to create a standalone application, using MATLAB API.
From what I found there is no way to read .mat file using "Data API", other than calling MATLAB engine, but that makes no point.
So how to do that without using MATLAB engine(standalone application)?
Why there is no support for C++ Data API for loading .mat files? Or at least for C API to read MATLAB String arrays?
Thank you for the feedback,
Maciej
  1 comentario
James Tursa
James Tursa el 24 de Oct. de 2019
Editada: James Tursa el 24 de Oct. de 2019
"There is no way to get value of the ".Name". I cannot do that even in a MEX file"
Can you elaborate? If you load a struct from a mat file and then use mxGetField, why doesn't that get you what you want? What else are you looking for? Are you trying to extract a C-style string from the mxArray?

Iniciar sesión para comentar.

Respuestas (1)

Maciej Panek
Maciej Panek el 24 de Oct. de 2019
James,
This is not a problem of getting a field from a structure, but getting a field value of type "MATLAB String" intruduced in r2016b:
There are two types of strings in MATLAB:
.Name = 'Some string' - which is a character vector, this is easy to get in C code.
.Name = "Some string" - which is string array, a new class intruduced in r2016b.
Let's say I have a MEX file, and I pass Header struct as first argument:
mxArray *header = mxGetField(prhs[0], 0, "Header");
mxArray *name = mxGetField(header, 0, "Name");
Then, when it was a character vector(single quoted string), I could do this:
mxGetString(name, buffer, len);
But, when it is a string array(double quoted string), there is no way to extract the value of the string from mxArray name.
I was able to get following snippet of code to work in C++ Mex:
StructArray header = inputs[0];
TypedArrayRef<MATLABString> inArrayNameRef = header[0]["Name"];
std::string Name = std::string(inArrayNameRef[0]);
But then, when you use C++(called MATLAB Data API), you can no longer use MATLAB C API to read data from .mat files, because it is based on mxArray implementation(old C API).
I cannot find MATLAB file API to load files using MATLAB Data API, which leads to the problem I described - there is no way to load string arrays(double quoted strings) from .mat files, using C or C++ API.
Maciej
  1 comentario
James Tursa
James Tursa el 24 de Oct. de 2019
I don't know the solution to this. The double quoted string class is an opaque object. You get practically no help in the API for getting at the underlying data of these objects. I am not sure the Engine would even work for you since I believe there may be restrictions on sending opaque objects through this COM interface. I don't even know how to hack into these type of variables to get at the data. If you had a mex routine, you could of course call back into MATLAB via mexCallMATLAB to turn it into a regular char array, but that won't help you if you are building a standalone program. Other than changing the method of saving the variables into the mat file so it saves them (or a copy of them) as regular char variables, I don't know how to help you.

Iniciar sesión para comentar.

Categorías

Más información sobre Call C++ from MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by