Cant read triangulation from struct with the C API

1 回表示 (過去 30 日間)
Pascal Reinhold
Pascal Reinhold 2023 年 5 月 23 日
回答済み: Sugandhi 2023 年 9 月 20 日
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 Center および File ExchangeSparse Matrices についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by