mp.mapped_array

class mp.mapped_array

Bases: handle

mp.mapped_array - Cell array indexed by name as well as numeric index.

Currently, arrays are only 1-D.

Example usage:

% create a mapped array object
ma = mp.mapped_array({30, 40, 50}, {'width', 'height', 'depth'});

% treat it like a cell array
ma{3} = 60;
height = ma{2};
for i = 1:length(ma)
    disp( ma{i} );
end

% treat it like a struct
ma.width = 20;
depth = ma.depth;

% add elements
ma.add_elements({'red', '25 lbs'}, {'color', 'weight'});

% delete elements
ma.delete_elements([3 5]);
ma.delete_elements('height');

% check for named element
ma.has_name('color');
mp.mapped_array Methods:
  • mapped_array() - constructor

  • copy() - create a duplicate of the mapped array object

  • length() - return number of elements in mapped array

  • size() - return dimensions of mapped array

  • add_names() - add or modify names of elements

  • add_elements() - append elements to the end of the mapped array

  • delete_elements() - delete elements from the mapped array

  • has_name() - return true if the name exists in the mapped array

  • name2idx() - return the index corresponding to a name

  • subsref() - called when indexing a mapped array to retrieve data

  • subsasgn() - called when indexing a mapped array to assign data

  • display() - display the mapped array structure

Constructor Summary
mapped_array(varargin)
obj = mp.mapped_array(vals)
obj = mp.mapped_array(vals, names)
Inputs:
  • vals (cell array) – values to be stored

  • names (cell array of char arrays) – names for each element in vals, where a valid name is any valid variable name that is not one of the methods of this class. If names are not provided, it is equivalent to a cell array, except that names can be added later.

Method Summary
copy()

Create a duplicate of the mapped array object.

new_obj = obj.copy();
length()

Return number of elements in mapped array.

num_elements = obj.length();
size(dim)

Return dimensions of mapped array. First dimension is 1, second matches the length.

[m, n] = obj.size();
m = obj.size(1);
n = obj.size(2);
add_names(i0, names)

Add or modify names of elements.

obj.add_names(i0, names)
Inputs:
  • i0 (cell array) – index of element corresponding to first name provided in names

  • names (char array or cell array of char arrays) – the names to assign

Adds or overwrites the names for elements starting at the specified index.

add_elements(vals, names)

Append elements to the end of the mapped array.

obj.add_elements(vals);
obj.add_elements(vals, names);
Inputs:
  • vals – single value or cell array of values

  • names (char array or cell array of char arrays) – (optional) corresponding names

The two arguments must be both cell arrays of the same dimension or a single value and single name.

See also delete_elements().

delete_elements(refs)

Delete elements from the mapped array.

obj.delete_elements(idx);
obj.delete_elements(names);
Inputs:
  • idx (scalar or vector integer) – index(indices) of element(s) to delete

  • names (char array or cell array of char arrays) – name(s) of element(s) to delete

See also add_elements().

has_name(name)

Return true if the name exists in the mapped array.

TorF = obj.has_name(name);
Input:

name (char array) – name to check

name2idx(name)

Return the numerical index in the array corrsponding to a name.

idx = obj.name2idx(name);
Input:

name (char array) – name corresponding to desired index

subsref(s)

Called when indexing a table to retrieve data.

val = obj.<name>;
val = obj{idx};
subsasgn(s, b)

Called when indexing a table to assign data.

obj.<name> = val;
obj{idx} = val;
display()

Display the mapped array structure.

This method is called automatically when omitting a semicolon on a line that retuns an object of this class.