Cant read triangulation from struct with the C API
2 views (last 30 days)
Show older comments
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 :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1391264/image.png)
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 Comment
Harimurali
on 7 Sep 2023
Edited: Harimurali
on 8 Sep 2023
Hi Pascal,
Could you share the .mat file from which you are trying to read the triangulation?
Answers (1)
Sugandhi
on 20 Sep 2023
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 Comments
See Also
Categories
Find more on Call C++ from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!