Cant read triangulation from struct with the C API

3 次查看(过去 30 天)
I want to read in a triangulation from a .mat-file for a tool that is outside of Matlab. When I try to read the following struct :
with the following code which just iterates over all fields in a struct and prints the field names and their class name if the field was not empty
// Print the class of each field in a structure
void analyzeStructArray(const mxArray *struct_ptr) {
mwSize n_fields; /* number of fields */
mwSize n_elements; /* number of elements in array */
mwIndex fIdx; /* field index */
const char *fname; /* field name */
const mxArray *fPtr; /* field pointer */
mxClassID category; /* class ID */
n_fields = mxGetNumberOfFields(struct_ptr);
for (mwSize field_id = 0; field_id < n_fields; field_id++) {
fname = mxGetFieldNameByNumber(struct_ptr, field_id);
fPtr = mxGetFieldByNumber(struct_ptr, 0, field_id);
if (fPtr == NULL) {
printf("Empty field: %s\n", fname);
} else {
auto class_id = mxGetClassID(fPtr);
const char * class_name = mxGetClassName(fPtr);
printf(" %s: of class %s\n", fname, class_name);
}
}
}
I get the following output:
5 fields; 1 elements
prob_net: of class double
labels_net: of class logical
labels: of class logical
Empty field: tri
Empty field: name
I just rechecked the .mat-File in Matlab, but fields are not empty. Could someone explain to me why this happens and how I can access those fields?
  1 个评论
Harimurali
Harimurali 2023-9-7
编辑:Harimurali 2023-9-8
Hi Pascal,
Could you share the .mat file from which you are trying to read the triangulation?

请先登录,再进行评论。

回答(1 个)

Sugandhi
Sugandhi 2023-9-20
Hi,
I understand that you are not able to read triangulation from struct with the C API.
mxArray *mxGetFieldByNumber(const mxArray *pm, mwIndex index, int fieldnumber);
Above code line returns pointer to the ‘mxArray’ in the specified field for the desired element, on success. Returns NULL in C if passed an invalid argument or if there is no value assigned to the specified field. Common causes of failure include:
  1. Specifying an array pointer pm that does not point to a structure mxArray. Call ‘mxIsStruct’ to determine whether pm points to a structure ‘mxArray’.
  2. Specifying an index to an element outside the bounds of the ‘mxArray’. For example, given a structure ‘mxArray’ that contains 10 elements, you cannot specify an index greater than 9 in C.
  3. Specifying a non-existent field number. Call ‘mxGetFieldNumber’ to determine the field number that corresponds to a given field name.
One of the possible workarounds could be checking if there is no value at index 0 assigned to the ‘tri’ field and update it.
For more understanding, kindly go through following link:

类别

Help CenterFile Exchange 中查找有关 Call C++ from MATLAB 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by