Quantcast

Documentation Center

  • Trial Software
  • Product Updates

mxGetField (C and Fortran)

Get field value from structure array, given index and field name

C Syntax

#include "matrix.h"
mxArray *mxGetField(const mxArray *pm, mwIndex index,
         const char *fieldname);

Fortran Syntax

mwPointer mxGetField(pm, index, fieldname)
mwPointer pm
mwIndex index
character*(*) fieldname

Arguments

pm

Pointer to a structure mxArray

index

Index of the desired element.

In C, the first element of an mxArray has an index of 0. The index of the last element is N-1, where N is the number of elements in the array. In Fortran, the first element of an mxArray has an index of 1. The index of the last element is N, where N is the number of elements in the array.

fieldname

Name of the field whose value you want to extract.

Returns

Pointer to the mxArray in the specified field at the specified fieldname, on success. Returns NULL in C (0 in Fortran) 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. To determine whether pm points to a structure mxArray, call mxIsStruct.

  • Specifying an index to an element outside the bounds of the mxArray. For example, given a structure mxArray that contains ten elements, you cannot specify an index greater than 9 in C (10 in Fortran).

  • Specifying a nonexistent fieldname. Call mxGetFieldNameByNumber or mxGetFieldNumber to get existing field names.

  • Insufficient heap space.

mxGetField allocates an empty, double mxArray if the pointer in the field is NULL.

Description

Call mxGetField to get the value held in the specified element of the specified field. In pseudo-C terminology, mxGetField returns the value at:

pm[index].fieldname

mxGetFieldByNumber is like mxGetField. Both functions return the same value. The only difference is in the way you specify the field. mxGetFieldByNumber takes a field number as its third argument, and mxGetField takes a field name as its third argument.

    Note   Inputs to a MEX-file are constant read-only mxArrays. Do not modify the inputs. Using mxSetCell* or mxSetField* functions to modify the cells or fields of a MATLAB® argument causes unpredictable results.

In C, calling:

mxGetField(pa, index, "field_name");

is equivalent to calling:

field_num = mxGetFieldNumber(pa, "field_name");
mxGetFieldByNumber(pa, index, field_num);

where index is 0 if you have a 1-by-1 structure.

In Fortran, calling:

mxGetField(pm, index, 'fieldname')

is equivalent to calling:

fieldnum = mxGetFieldNumber(pm, 'fieldname')
mxGetFieldByNumber(pm, index, fieldnum)

where index is 1 if you have a 1-by-1 structure.

Was this topic helpful?