Example #1
0
func (f *File) readStruct(array *C.mxArray, ivalue reflect.Value) error {
	if C.mxSTRUCT_CLASS != C.mxGetClassID(array) {
		return errors.New("expected to find a structure")
	}

	if C.mxGetM(array)*C.mxGetN(array) != 1 {
		return errors.New("expected to find one structure")
	}

	typo := ivalue.Type()
	count := typo.NumField()

	if count != int(C.mxGetNumberOfFields(array)) {
		return errors.New("expected to find all fields of the structure")
	}

	for i := 0; i < count; i++ {
		field := typo.Field(i)

		name := C.CString(field.Name)
		defer C.free(unsafe.Pointer(name))

		farray := C.mxGetField(array, 0, name)
		if farray == nil {
			return errors.New("cannot read a field of the structure")
		}

		if err := f.readObject(farray, ivalue.Field(i)); err != nil {
			return err
		}
	}

	return nil
}
Example #2
0
func (f *File) readArray(array *C.mxArray, ivalue reflect.Value) error {
	parray := unsafe.Pointer(C.mxGetPr(array))
	if parray == nil {
		return errors.New("cannot read the variable")
	}

	count := C.mxGetM(array) * C.mxGetN(array)
	size, ok := classSizeMapping[C.mxGetClassID(array)]
	if !ok {
		return errors.New("encountered an unsupported data type")
	}

	if ivalue.Kind() == reflect.Slice {
		return readSlice(ivalue, parray, count, size)
	} else {
		if count != 1 {
			return errors.New("expected to find a scalar")
		}
		return readScalar(ivalue, parray)
	}
}