insert - Add entries to a dictionary - MATLAB (original) (raw)

Add entries to a dictionary

Since R2023b

Syntax

Description

d2 = insert([d1](#mw%5F244b0144-fdba-4be6-9398-3a00faec7089),[key](#mw%5F56de60b8-1ac4-4215-9dec-be959c6eca4f),[value](#mw%5Fec3a6deb-25ec-49a4-8a4d-a0caccf9c931)) assigns the value to key in dictionary,d2. If key already has a corresponding value, then insert overwrites the value.

d = insert(d,key,value) is equivalent to d(key) = value.

example

d2 = insert([d1](#mw%5F244b0144-fdba-4be6-9398-3a00faec7089),[key](#mw%5F56de60b8-1ac4-4215-9dec-be959c6eca4f),[value](#mw%5Fec3a6deb-25ec-49a4-8a4d-a0caccf9c931),Overwrite=[tf](#mw%5F093493b8-644e-4d5b-a339-bbf1520e9fb2)) specifies whether to overwrite an existing value corresponding tokey.

example

Examples

collapse all

Create a dictionary containing several key-value pairs.

names = ["Unicycle" "Bicycle" "Tricycle"]; wheels = [1 2 3]; d = dictionary(wheels,names)

d =

dictionary (double ⟼ string) with 3 entries:

1 ⟼ "Unicycle"
2 ⟼ "Bicycle"
3 ⟼ "Tricycle"

Insert a new entry.

d =

dictionary (double ⟼ string) with 4 entries:

1 ⟼ "Unicycle"
2 ⟼ "Bicycle"
3 ⟼ "Tricycle"
4 ⟼ "Car"

Create a dictionary containing several key-value pairs.

names = ["Unicycle" "Bicycle" "Tricycle"]; wheels = [1 2 3]; d = dictionary(wheels,names)

d =

dictionary (double ⟼ string) with 3 entries:

1 ⟼ "Unicycle"
2 ⟼ "Bicycle"
3 ⟼ "Tricycle"

Insert new entries without overwriting existing entries.

d = insert(d,[2 4],["Motorcycle" "Car"],Overwrite=false)

d =

dictionary (double ⟼ string) with 4 entries:

1 ⟼ "Unicycle"
2 ⟼ "Bicycle"
3 ⟼ "Tricycle"
4 ⟼ "Car"

Input Arguments

collapse all

Input dictionary, specified as a dictionary object.

Key set, specified as a scalar or an array. The data type of key must match or be convertible to the data type of keys in d. The size of key must be compatible with the size ofvalue.

Value set, specified as a scalar or an array. The data type ofvalue must match or be convertible to the data type of values ind. The size of key must be compatible with the size of value.

Option to overwrite existing entries, specified as true,false, 1 or 0. Specify a value of 0 or false to preventinsert from overwriting existing entries.

Example: Overwrite=false

Extended Capabilities

expand all

Usage notes and limitations:

For additional considerations that apply when generating C/C++ code for MATLAB® dictionaries, see Dictionary Limitations for Code Generation (MATLAB Coder).

Version History

Introduced in R2023b

expand all

You can generate C/C++ code for the dictionary insert function.