필터 지우기
필터 지우기

Cant read triangulation from struct with the C API

조회 수: 3 (최근 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 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