
- •Matlab r2013a стр. 225
- •Continue Long Statements on Multiple Lines
- •Creating and Concatenating Matrices
- •Overview
- •Constructing a Simple Matrix
- •Entering Signed Numbers
- •Specialized Matrix Functions
- •Examples
- •Concatenating Matrices
- •Keeping Matrices Rectangular
- •Matrix Concatenation Functions
- •Examples
- •Generating a Numeric Sequence
- •The Colon Operator
- •Using the Colon Operator with a Step Value
- •Matrix Indexing
- •Accessing Single Elements
- •Linear Indexing
- •Functions That Control Indexing Style
- •Accessing Multiple Elements
- •Nonconsecutive Elements
- •The end Keyword
- •Specifying All Elements of a Row or Column
- •Using Logicals in Array Indexing
- •Logical Indexing – Example 1
- •Logical Indexing – Example 2
- •Logical Indexing with a Smaller Array
- •Single-Colon Indexing with Different Array Types
- •Indexing on Assignment
- •Arithmetic Operators
- •Arithmetic Operators and Arrays
- •Operator Precedence
- •Precedence of and and or Operators
- •Overriding Default Precedence
- •Relational Operators and Arrays
- •Relational Operators and Empty Arrays
- •Overview of the Logical Class
- •Logical Operators
- •Element-Wise Operators and Functions
- •Short-Circuit Operators
- •Precedence of and and or Operators
- •Symbol Reference
- •Asterisk — *
- •Filename Wildcard
- •Function Handle Constructor
- •Class Folder Designator
- •Line Continuation
- •Dynamic Structure Fields
- •Exclamation Point — !
- •Semicolon — ;
- •Array Row Separator
- •Output Suppression
- •Command or Statement Separator
- •Single Quotes — ' '
- •Square Brackets — [ ]
- •Fundamental matlab Classes
- •More About
- •Overview of Numeric Classes
- •Integers
- •Integer Classes
- •Creating Integer Data
- •Arithmetic Operations on Integer Classes
- •Largest and Smallest Values for Integer Classes
- •Integer Functions
- •Floating-Point Numbers
- •Double-Precision Floating Point
- •Single-Precision Floating Point
- •Creating Floating-Point Data
- •Creating Double-Precision Data
- •Creating Single-Precision Data
- •Arithmetic Operations on Floating-Point Numbers
- •Double-Precision Operations
- •Single-Precision Operations
- •Largest and Smallest Values for Floating-Point Classes
- •Largest and Smallest Double-Precision Values
- •Largest and Smallest Single-Precision Values
- •Accuracy of Floating-Point Data
- •Double-Precision Accuracy
- •Single-Precision Accuracy
- •Avoiding Common Problems with Floating-Point Arithmetic
- •Example 1 — Round-Off or What You Get Is Not What You Expect
- •Example 2 — Catastrophic Cancellation
- •Example 3 — Floating-Point Operations and Linear Algebra
- •Floating-Point Functions
- •Creating a Rectangular Character Array
- •Combining Strings Vertically
- •Combining Strings Horizontally
- •Identifying Characters in a String
- •Working with Space Characters
- •Expanding Character Arrays
- •String Comparisons
- •Comparing Strings for Equality
- •Comparing for Equality Using Operators
- •Categorizing Characters Within a String
- •Create a Structure Array
- •Access Data in a Structure Array
- •Concatenate Structures
- •Generate Field Names from Variables
- •Access Data in Nested Structures
- •Access Elements of a Nonscalar Struct Array
- •Create a Cell Array
- •Access Data in a Cell Array
- •Add Cells to a Cell Array
- •Delete Data from a Cell Array
- •Combine Cell Arrays
- •Pass Contents of Cell Arrays to Functions
- •Multilevel Indexing to Access Parts of Cells
- •Related Examples
- •What Is a Function Handle?
- •Creating a Function Handle
- •Maximum Length of a Function Name
- •The Role of Scope, Precedence, and Overloading When Creating a Function Handle
- •Obtaining Permissions from Class Methods
- •Example
- •Using Function Handles for Anonymous Functions
- •Arrays of Function Handles
- •Calling a Function Using Its Handle
- •Calling Syntax
- •Calling a Function with Multiple Outputs
- •Returning a Handle for Use Outside of a Function File
- •Example — Using Function Handles in Optimization
- •Preserving Data from the Workspace
- •Preserving Data with Anonymous Functions
- •Preserving Data with Nested Functions
- •Loading a Saved Handle to a Nested Function
- •Applications of Function Handles
- •Example of Passing a Function Handle
- •Pass a Function to Another Function
- •Example 1 — Run integral on Varying Functions
- •Example 2 — Run integral on Anonymous Functions
- •Example 3 — Compare integral Results on Different Functions
- •Capture Data Values For Later Use By a Function
- •Example 1 — Constructing a Function Handle that Preserves Its Variables
- •Example 2 — Varying Data Values Stored in a Function Handle
- •Example 3 — You Cannot Vary Data in a Handle to an Anonymous Function
- •Call Functions Outside of Their Normal Scope
- •Save the Handle in a mat-File for Use in a Later matlab Session
- •Parameterizing Functions
- •Overview
- •Parameterizing Using Nested Functions
- •Parameterizing Using Anonymous Functions
- •See Also
- •More About
- •Saving and Loading Function Handles
- •Invalid or Obsolete Function Handles
- •Advanced Operations on Function Handles
- •Examining a Function Handle
- •Converting to and from a String
- •Converting a String to a Function Handle
- •Converting a Function Handle to a String
- •Comparing Function Handles
- •Comparing Handles Constructed from a Named Function
- •Comparing Handles to Anonymous Functions
- •Comparing Handles to Nested Functions
- •Comparing Handles Saved to a mat-File
- •Overview of the Map Data Structure
- •Description of the Map Class
- •Properties of the Map Class
- •Methods of the Map Class
- •Creating a Map Object
- •Constructing an Empty Map Object
- •Constructing An Initialized Map Object
- •Combining Map Objects
- •Examining the Contents of the Map
- •Reading and Writing Using a Key Index
- •Reading From the Map
- •Adding Key/Value Pairs
- •Building a Map with Concatenation
- •Modifying Keys and Values in Map
- •Removing Keys and Values from the Map
- •Modifying Values
- •Modifying Keys
- •Modifying a Copy of the Map
- •Mapping to Different Value Types
- •Mapping to a Structure Array
- •Mapping to a Cell Array
R2013a>MATLAB>Language Fundamentals>Data Types>Map Containers
Reading and Writing Using a Key Index
On this page… |
Reading From the Map Adding Key/Value Pairs Building a Map with Concatenation |
When reading from the Map, use the same keys that you have defined and associated with particular values. Writing new entries to the Map requires that you supply the values to store with a key for each one .
Note: For a large Map, the keys and value methods use a lot of memory as their outputs are cell arrays. |
Reading From the Map
After you have constructed and populated your Map, you can begin to use it to store and retrieve data. You use a Map in the same manner that you would an array, except that you are not restricted to using integer indices. The general syntax for looking up a value (valueN) for a given key (keyN) is shown here. If the key is a character string, enclose it in single quotation marks:
valueN = mapObj(keyN);
You can find any single value by indexing into the map with the appropriate key:
passenger = ticketMap('2R175')
passenger =
James Enright
Find the person who holds ticket A479GY:
sprintf(' Would passenger %s please come to the desk?\n', ...
ticketMap('A479GY'))
ans =
Would passenger Sarah Latham please come to the desk?
To access the values of multiple keys, use the values method, specifying the keys in a cell array:
values(ticketMap, {'2R175', 'B7398'})
ans =
'James Enright' 'Carl Haynes'
Map containers support scalar indexing only. You cannot use the colon operator to access a range of keys as you can with other MATLAB® classes. For example, the following statements throw an error:
ticketMap('2R175':'B7398')
ticketMap(:)
Adding Key/Value Pairs
Unlike other array types, each entry in a Map consists of two items: the value and its key. When you write a new value to a Map, you must supply its key as well. This key must be consistent in type with any other keys in the Map.
Use the following syntax to insert additional elements into a Map:
existingMapObj(newKeyName) = newValue;
Add two more entries to the ticketMap used in the above examples, Verify that the Map now has five key/value pairs:
ticketMap('947F4') = 'Susan Spera';
ticketMap('417R93') = 'Patricia Hughes';
ticketMap.Count
ans =
6
List all of the keys and values in Map ticketMap:
keys(ticketMap), values(ticketMap)
ans =
'2R175' '417R93' '947F4' 'A479GY' 'B7398' 'NZ1452'
ans =
Columns 1 through 3
'James Enright' 'Patricia Hughes' 'Susan Spera'
Columns 4 through 6
'Sarah Latham' 'Carl Haynes' 'Bradley Reid'
Building a Map with Concatenation
You can add key/value pairs to a Map in groups using concatenation. The concatenation of Map objects is different from other classes. Instead of building a vector of s, MATLAB returns a single Map containing the key/value pairs from each of the contributing Map objects.
Rules for the concatenation of Map objects are:
Only vertical vectors of Map objects are allowed. You cannot create an m-by-n array or a horizontal vector of s. For this reason, vertcat is supported for Map objects, but not horzcat.
All keys in each map being concatenated must be of the same class.
You can combine Maps with different numbers of key/value pairs. The result is a single Map object containing key/value pairs from each of the contributing maps:
tMap1 = containers.Map({'2R175', 'B7398', 'A479GY'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham'});
tMap2 = containers.Map({'417R93', 'NZ1452', '947F4'}, ...
{'Patricia Hughes', 'Bradley Reid', 'Susan Spera'});
% Concatenate the two maps:
ticketMap = [tMap1; tMap2];
The result of this concatenation is the same 6-element map that was constructed in the previous section:
ticketMap.Count
ans =
6
keys(ticketMap), values(ticketMap)
ans =
'2R175' '417R93' '947F4' 'A479GY' 'B7398' 'NZ1452'
ans =
Columns 1 through 3
'James Enright' 'Patricia Hughes' 'Susan Spera'
Columns 4 through 6
'Sarah Latham' 'Carl Haynes' 'Bradley Reid'
Concatenation does not include duplicate keys or their values in the resulting Map object.
In the following example, both objects m1 and m2 use a key of 8. In Map m1, 8 is a key to value C; in m2, it is a key to value X:
m1 = containers.Map({1, 5, 8}, {'A', 'B', 'C'});
m2 = containers.Map({8, 9, 6}, {'X', 'Y', 'Z'});
Combine m1 and m2 to form a new Map object, m:
m = [m1; m2];
The resulting Map object m has only five key/value pairs. The value C was dropped from the concatenation because its key was not unique:
keys(m), values(m)
ans =
[1] [5] [6] [8] [9]
ans =
'A' 'B' 'Z' 'X' 'Y'