add array to dictionary in matlab

To add an array to a dictionary in MATLAB, you can use the containers.Map class.

First, create an instance of the containers.Map class by specifying the key value pairs. Here, the keys are the string values that you want to use as the dictionary keys, and the values are the array that you want to add to the dictionary.

main.m
% create a dictionary 
myDict = containers.Map({'key1', 'key2', 'key3'}, {[1,2,3], [4,5,6], [7,8,9]});
103 chars
3 lines

Now, you can access the values in the dictionary using their keys:

main.m
% access values using keys 
myDict('key1') % returns [1, 2, 3]
myDict('key2') % returns [4, 5, 6]
myDict('key3') % returns [7, 8, 9]
133 chars
5 lines

To add a new key value pair to the dictionary, you can directly assign the key and value to the dictionary:

main.m
% add a new key value pair 
myDict('key4') = [10, 11, 12];
59 chars
3 lines

Now, you can access the new key value pair as well:

main.m
% access the newly added key value pair 
myDict('key4') % returns [10, 11, 12]
79 chars
3 lines

related categories

gistlibby LogSnag