insert

Adds a key and value to a dictionary and returns the updated dictionary.

Category: Language Fundamentals > Data Types

Syntax

d2 = insert(d, key, value)

Adds the pair, or overwrites the value if key already exists.

Description

A dictionary is a value, not a handle object, so the result must be assigned: d = insert(d, k, v). This is equivalent to d(k) = v. The key type must match the types the dictionary already holds.

Input arguments

d
dictionary object.
key, value
The key and its value, or arrays of matching length for several pairs.

Output arguments

d2
Updated dictionary. The input d is not modified.

Example

Add and overwrite

d = dictionary(["x" "y"], [1 2]);
d = insert(d, "z", 3)
d = insert(d, "x", 99);
d("x")
Output
d = 
  dictionary (string --> double) with 3 entries:

    "x" --> 1
    "y" --> 2
    "z" --> 3
ans = 99

See also

dictionary isConfigured keys values isKey remove