Ejemplo n.º 1
0
func (f *File) writeArray(value reflect.Value, dimensions ...uint) (*C.mxArray, error) {
	var kind reflect.Kind

	if value.Kind() == reflect.Slice {
		kind = value.Type().Elem().Kind()
	} else {
		kind = value.Kind()
	}

	classid, ok := kindClassMapping[kind]
	if !ok {
		return nil, errors.New("encountered an unsupported data type")
	}

	// NOTE: Do we need a proper conversion from uint to C.size_t?
	array := C.mxCreateNumericArray(C.size_t(len(dimensions)),
		(*C.size_t)(unsafe.Pointer(&dimensions[0])), classid, C.mxREAL)
	if array == nil {
		return nil, errors.New("cannot create an array")
	}

	parray := unsafe.Pointer(C.mxGetPr(array))
	if parray == nil {
		C.mxDestroyArray(array)
		return nil, errors.New("cannot create an array")
	}

	size, ok := classSizeMapping[classid]
	if !ok {
		return nil, errors.New("encountered an unsupported data type")
	}

	if value.Kind() == reflect.Slice {
		if err := writeSlice(value, parray, size); err != nil {
			C.mxDestroyArray(array)
			return nil, err
		}
	} else {
		if err := writeScalar(value, parray); err != nil {
			C.mxDestroyArray(array)
			return nil, err
		}
	}

	return array, nil
}
Ejemplo n.º 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)
	}
}