Cant read triangulation from struct with the C API
1 次查看(过去 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
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
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:
- Specifying an array pointer pm that does not point to a structure mxArray. Call ‘mxIsStruct’ to determine whether pm points to a structure ‘mxArray’.
- 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.
- 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:
- ‘mxGetFieldByNumber’: https://www.mathworks.com/help/matlab/apiref/mxgetfieldbynumber.html?&s_tid=srchtitle_support_results_1_mxGetFieldByNumber
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!