public class MWStructArray extends MWArray
MWStructArray
class manages a native MATLAB struct array.EMPTY_ARRAY
Constructor and Description |
---|
MWStructArray()
Creates an empty struct array.
|
MWStructArray(int[] dims,
java.lang.String[] fieldnames)
Constructs a new struct array with the specified dimensions and field names.
|
MWStructArray(int rows,
int cols,
java.lang.String[] fieldnames)
Constructs a new struct matrix with the specified number of rows and columns and field names.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
applyVisitor(AbstractMWArrayVisitor<T> v) |
MWClassID |
classID()
Returns the MATLAB type of this array.
|
java.lang.Object |
clone()
Creates and returns a deep copy of this array.
|
int[] |
columnIndex()
Returns an array containing the column index of each element in the underlying MATLAB array.
|
int |
compareTo(java.lang.Object obj)
Compares this array with the specified array for order.
|
static MWArray |
deserialize(byte[] data)
Create a new MWArray from serialized data.
|
void |
dispose()
Frees the native MATLAB array contained by this array.
|
boolean |
equals(java.lang.Object obj)
Indicates whether some other array is equal to this one.
|
int |
fieldIndex(java.lang.String fieldname) |
java.lang.String[] |
fieldNames()
Returns the field names in this array.
|
static MWStructArray |
fromBean(java.lang.Object bean) |
static MWStructArray |
fromMap(java.util.Map<java.lang.String,java.lang.Object> map) |
static MWStructArray |
fromProperties(java.util.Properties props) |
java.lang.Object |
get(int index)
Returns the element at the specified 1-based offset in this array.
|
java.lang.Object |
get(int[] index)
Returns the element at the specified 1-based index-array in this array.
|
java.lang.Object |
get(java.lang.String fieldname,
int index)
Returns the element at the specified 1-based offset and field name in this array.
|
java.lang.Object |
get(java.lang.String fieldname,
int[] index)
Returns the element at the specified 1-based index-array and field name in this array.
|
java.lang.Object |
getData()
Returns a 1-D array containing a copy of the data in the underlying MATLAB array.
|
int[] |
getDimensions()
Returns an array containing the size of each dimension of this array.
|
MWArray |
getField(int index)
Returns the element at the specified 1-based offset in this array as an
MWArray instance. |
MWArray |
getField(int[] index)
Returns a shared copy of the element at the specified 1-based index-array in this array as an
MWArray instance. |
MWArray |
getField(java.lang.String fieldname,
int index)
Returns a shared copy of the element at the specified 1-based offset and field name in this array as an
MWArray instance. |
MWArray |
getField(java.lang.String fieldname,
int[] index)
Returns a shared copy of the element at the specified 1-based index-array and field name in this array as an
MWArray instance. |
int |
hashCode()
Returns a hash code value for this array.
|
boolean |
isEmpty()
Tests if this array has no elements.
|
boolean |
isSparse()
Tests if this array is sparse.
|
int |
maximumNonZeros()
Returns the allocated capacity of a sparse array.
|
int |
numberOfDimensions()
Returns the number of dimensions of this array.
|
int |
numberOfElements()
Returns the total number of elements in this array.
|
int |
numberOfFields() |
int |
numberOfNonZeros()
Returns the number of non-zero elements in a sparse array.
|
protected java.lang.Object |
readResolve()
Called by serialization mechanism when loading a new array from a byte stream.
|
int[] |
rowIndex()
Returns an array containing the row index of each element in the underlying MATLAB array.
|
byte[] |
serialize()
Serialize the MATLAB array to a byte array.
|
void |
set(int[] index,
java.lang.Object element)
Replaces the element at the specified 1-based index-array in this array with the specified element.
|
void |
set(int index,
java.lang.Object element)
Replaces the element at the specified 1-based offset in this array with the specified element.
|
void |
set(java.lang.String fieldname,
int[] index,
java.lang.Object element)
Replaces the element at the specified 1-based index-array and field name in this array with the specified element.
|
void |
set(java.lang.String fieldname,
int index,
java.lang.Object element)
Replaces the element at the specified 1-based offset and field name in this array with the specified element.
|
void |
setData(java.lang.Object data) |
java.lang.Object |
sharedCopy()
Creates and returns a shared copy of this array.
|
java.lang.Object[] |
toArray()
Returns an array containing a copy of the data in the underlying MATLAB array.
|
java.lang.String |
toString()
Returns a string representation of this array.
|
protected void |
validate()
Validates the internal array handle.
|
disposeArray
public MWStructArray()
Example: Constructing a Structure Array Object
This example creates a 0-by-0 MWStructArray object:MWStructArray S = new MWStructArray(); System.out.println("Structure array S: " + S);When run, the example displays this output:
Structure array S: []
public MWStructArray(int[] dims, java.lang.String[] fieldnames)
dims
- Array of dimension sizes. Each dimension size must be non-negative.fieldnames
- Array of field names.java.lang.NegativeArraySizeException
- A negative dimension size is supplied.
Example: Constructing a Structure Array Object
This example creates a 1-by-2 MWStructArray object with fields f1, f2, and f3:String[] sfields = {"f1", "f2", "f3"}; int[] dims = new int[]{1,2}; MWStructArray S = new MWStructArray(dims, sfields); System.out.println("Structure array S: " + S);When run, the example displays this output:
Structure array S: 1x2 struct array with fields: f1 f2 f3
public MWStructArray(int rows, int cols, java.lang.String[] fieldnames)
rows
- Number of rows. Number of rows must be non-negative.cols
- Number of columns. Number of columns must be non-negative.fieldnames
- Array of field names.java.lang.NegativeArraySizeException
- A negative row or column size is supplied.
Example: Constructing a Structure Array Object
This example creates a 1-by-2 MWStructArray object with fields f1, f2, and f3:int rows = 1, cols = 2; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(rows, cols, sfields); System.out.println("Structure array S: " + S);When run, the example displays this output:
Structure array S: 1x2 struct array with fields: f1 f2 f3
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
MWStructArray
instance representing a deep copy of the
underlying MATLAB array.
Example: Cloning a Structure Array Object
Create an MWStructArray object and then a clone of that object:int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); Object C = S.clone(); System.out.println("Clone of structure S is:"); System.out.println(C.toString());When run, the example displays this output:
Clone of structure S is: 1x2 struct array with fields: f1 f2 f3
java.lang.CloneNotSupportedException
- - The object's class does not implement the Cloneable interface.public java.lang.Object sharedCopy()
MWStructArray
instance representing a shared copy of the
underlying MATLAB array.
Example: Making a Shared Copy of a Structure Array Object
Create an MWStructArray object and then a shared copy of that object:int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); Object C = S.sharedCopy(); System.out.println("Shared copy of structure S is:"); System.out.println(C.toString());When run, the example displays this output:
Shared copy of structure S is: 1x2 struct array with fields: f1 f2 f3
public MWClassID classID()
MWClassID.STRUCT
for MWStructArray
.
Example: Getting the Class ID of a Structure Array
Create an MWStructArray object and display the class ID:int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); System.out.println("The class of S is " + S.classID());When run, the example displays this output:
The class of S is struct
public int numberOfFields()
Example: Getting the Number of Fields in a Structure Array
Create an MWStructArray object with three fields and display the number of fields:int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); String[] str = S.fieldNames(); System.out.println("There are " + S.numberOfFields() + " fields in this structure.");When run, the example displays this output:
There are 3 fields in this structure.
public java.lang.String[] fieldNames()
Example: Getting the Field Names of a Structure Array
Create an MWStructArray object with three fields and display the field names:int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); String[] str = S.fieldNames(); System.out.print("The structure has the fields: "); for (int i=0; i < S.numberOfFields(); i++) System.out.print(" " + str[i]);When run, the example displays this output:
The structure has the fields: f1 f2 f3
public int fieldIndex(java.lang.String fieldname)
fieldname
- the name of the field to look uppublic void set(int index, java.lang.Object element)
index
- The index of the element to replace. Valid range: 1 <= index <= N, where N = total
number of elements in the array. The total number of elements for a struct array = numberOfElements()
*numberOfFields()
.element
- New element to replace at index. If element is of type MWArray
, the cell
at index set to a shared copy of the underlying MATLAB array. Otherwise, a new MATLAB array is created
from element using default conversion rules and assigned to the cell at index.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.
Example: Setting Values in a Structure Array
int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); int totNumElem = S.numberOfElements() * S.numberOfFields(); for (int i = 1; i <= totNumElem; i++) { S.set(i,i*15); } int elemCnt = 1; for(int r=1; r <= sdims[0]; r++) { for(int c=1; c <= sdims[1]; c++) { System.out.println("Element : " + elemCnt++); for(int f=0; f < sfields.length; f++) { System.out.print("\t" + sfields[f] + " : "); System.out.println(S.getField(sfields[f], new int[]{r,c})); } } }When run, the example displays this output:
Element : 1 f1 : 15 f2 : 30 f3 : 45 Element : 2 f1 : 60 f2 : 75 f3 : 90
public void set(int[] index, java.lang.Object element)
index
- Array of indices specifying the location of the element to replace.
The length of the index array must be exactly numberOfDimensions()
+ 1.
The first element of the index array is the 1-based field number with valid range 1 <= index[0] <= numberOfFields()
.
The remaining entries have valid ranges: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.element
- New element to replace at index. If element is of type MWArray
, the cell
at index set to a shared copy of the underlying MATLAB array. Otherwise, a new MATLAB array is created
from element using default conversion rules and assigned to the cell at index.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public void set(java.lang.String fieldname, int[] index, java.lang.Object element)
fieldname
- The field name of the element to replace.index
- Array of indices specifying the location of the element to replace.
The length of the index array must be exactly numberOfDimensions()
.
The entries of the index array have valid ranges: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.element
- New element to replace at index. If element is of type MWArray
, the cell
at index set to a shared copy of the underlying MATLAB array. Otherwise, a new MATLAB array is created
from element using default conversion rules and assigned to the cell at index.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public void set(java.lang.String fieldname, int index, java.lang.Object element)
fieldname
- The field name of the element to replace.index
- The index of the element to replace. Valid range: 1 <= index <= N, where N = numberOfElements()
element
- New element to replace at index. If element is of type MWArray
, the cell
at index set to a shared copy of the underlying MATLAB array. Otherwise, a new MATLAB array is created
from element using default conversion rules and assigned to the cell at index.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public java.lang.Object get(int index)
getField(index).toArray()
.index
- of requested element. Valid range: 1 <= index <= N, where N = total
number of elements in the array. The total number of elements for a struct array = numberOfElements()
*numberOfFields()
.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.
Example: Getting Structure Array Data with get
int[] cdims = {1, 3}; MWStructArray C = new MWStructArray(cdims); Integer val = new Integer(15); int[] index2 = {1, 3}; C.set(index2, val); Object x = C.get(index2); if (x instanceof int[][]) { int[][] y = (int[][])x; System.out.println("B: Cell data C(1,3) is " + y[0][0]); }When run, the example displays this output:
B: Cell data C(1,3) is 15
public java.lang.Object get(int[] index)
getField(index).toArray()
.index
- Array of indices specifying the location of requested element.
The length of the index array must be exactly numberOfDimensions()
+ 1.
The first element of the index array is the 1-based field number with valid range 1 <= index[0] <= numberOfFields()
.
The remaining entries have valid ranges: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public java.lang.Object get(java.lang.String fieldname, int[] index)
getField(fieldname, index).toArray()
.fieldname
- The field name of the requested element.index
- Array of indices specifying the location of the requested element.
The length of the index array must be exactly numberOfDimensions()
.
The entries of the index array have valid ranges: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public java.lang.Object get(java.lang.String fieldname, int index)
getField(fieldname, index).toArray()
.fieldname
- The field name of the requested element.index
- of the requested element.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public MWArray getField(int index)
MWArray
instance.index
- of requested element. Valid range: 1 <= index <= N, where N = total
number of elements in the array. The total number of elements for a struct array = numberOfElements()
*numberOfFields()
.MWArray
instance representing the requested field. This MWArray
reference
should be disposed by calling MWArray.dispose()
.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public MWArray getField(int[] index)
MWArray
instance.index
- Array of indices specifying the location of requested element.
The length of the index array must be exactly numberOfDimensions()
+ 1.
The first element of the index array is the 1-based field number with valid range 1 <= index[0] <= numberOfFields()
.
The remaining entries have valid ranges: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.MWArray
instance representing the requested field. This MWArray
reference
should be disposed by calling MWArray.dispose()
.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public MWArray getField(java.lang.String fieldname, int[] index)
MWArray
instance.fieldname
- The field name of the requested element.index
- Array of indices specifying the location of the requested element.
The length of the index array must be exactly numberOfDimensions()
.
The entries of the index array have valid ranges: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.MWArray
instance representing the requested field. This MWArray
reference
should be disposed by calling MWArray.dispose()
.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public MWArray getField(java.lang.String fieldname, int index)
MWArray
instance.fieldname
- The field name of the requested element.index
- of the requested element. Valid range: 1 <= index <= numberOfElements()
.MWArray
instance representing the requested field. This MWArray
reference
should be disposed by calling MWArray.dispose()
.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public java.lang.Object getData()
MWArray.toArray()
on the corresponding cell.
Example: Getting Structure Array Data with getData
Get the data stored in all fields and indices of MWStructArray object S:int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); int count = S.numberOfElements() * S.numberOfFields(); // Initialize the structure. Integer[] val = new Integer[6]; for (int i = 0; i < count; i++) val[i] = new Integer((i+1) * 15); // Use getData to get data from the structure. System.out.println("Data read from structure array S: \n"); MWArray[] x = (MWArray[]) S.getData(); for (int i = 0; i < x.length; i++) System.out.print(" " + x[i]);When run, the example displays this output:
Data read from structure array S: 15 30 45 60 75 90
public java.lang.Object[] toArray()
MWArray.toArray()
on the corresponding cell.
Example: Getting Structure Array Data with toArray
int[] sdims = {1, 2}; String[] sfields = {"f1", "f2", "f3"}; MWStructArray S = new MWStructArray(sdims, sfields); Integer[] val = new Integer[25]; for (int i = 0; i < 6; i++) val[i] = new Integer(i * 15); for (int i = 0; i < 2; i++) for (int j = 0; j < sfields.length; j++) S.set(sfields[j], i+1, val[j + (i * 3)]); Object[][][] x = (Object[][][]) S.toArray(); System.out.println(); System.out.println("Data read from structure array S \n"); for (int j = 0; j < 2; j++) for (int i = 0; i < x.length; i++) System.out.print(" " + ((int[][]) x[i][0][j])[0][0]);When run, the example displays this output:
Data read from structure array S 0 15 30 45 60 75
public <T> T applyVisitor(AbstractMWArrayVisitor<T> v)
protected void validate()
public static MWStructArray fromMap(java.util.Map<java.lang.String,java.lang.Object> map) throws MWException
MWException
public static MWStructArray fromBean(java.lang.Object bean) throws java.beans.IntrospectionException, MWException
java.beans.IntrospectionException
MWException
public static MWStructArray fromProperties(java.util.Properties props)
public byte[] serialize() throws java.io.IOException
java.io.IOException
- if the array cannot be serialized. For
example, executable content (function handles, class instances) cannot
be serialized.public static MWArray deserialize(byte[] data)
data
- serialized array returned from MWArray.serializepublic int numberOfDimensions()
numberOfDimensions
in class MWArray
public int[] getDimensions()
getDimensions
in class MWArray
public boolean isEmpty()
public boolean isSparse()
public boolean equals(java.lang.Object obj)
public int compareTo(java.lang.Object obj)
public int hashCode()
public java.lang.String toString()
public int numberOfElements()
numberOfElements
in class MWArray
public int numberOfNonZeros()
numberOfElements()
.numberOfNonZeros
in class MWArray
public int maximumNonZeros()
numberOfElements()
.maximumNonZeros
in class MWArray
public void dispose()
dispose
in interface Disposable
dispose
in class MWArray
public int[] rowIndex()
public int[] columnIndex()
columnIndex
in class MWArray
protected java.lang.Object readResolve() throws java.io.ObjectStreamException
java.io.InvalidObjectException
- Attempt to load an invalid array handle.java.io.ObjectStreamException
© 1994-2017 The MathWorks, Inc. Patents Trademarks