I have some data associated with several "labels" ('Jan','Feb','Mar', for example), and for each of these labels there are multiple data types (numeric arrays, strings, datetime, etc) for each.
I would like to access the data by specifying the label itself, so I don't have to keep track of any integer indices. I have recently discovered map containers, and tried to implement it in this case by using my "labels" as the keys in the map:
keys = {'Jan','Feb','Mar'};data_map = containers.Map('KeyType','char','ValueType','any');% loop over keys and put some data in the map
for k = 1:length(keys) data_struct.prices = rand(1,5); % create some dummy numbers
data_struct.description = 'HIGH'; % create a dummy label
% place the structure in the map at the relevant key
data_map(keys{k}) = data_struct; % delete ready for next iteration of the loop
clear data_structend
This works nicely, because the structure object is able to handle all the different data types, and I can very compactly and nicely access my data without any integers using:
data_map('Feb').description
My Problem
1) I feel like I am not using it in the correct way, because I have had to use the temporary structure data_struct inside the loop.
2) Additionally, later on I would now like to add an extra field to the structure associated with one of the keys, and would ideally do something like the following for example:
data_map('Feb').temperature = rand(1,8);
This gives the following error:
Only one level of indexing is supported by a containers.Map.
The only way I have been able to do it is to create a temporary structure, which is ugly and cumbersome:
temp_struct = data_map('Feb');temp_struct.temperature = rand(1,8);data_map('Feb') = temp_struct;clear temp_structdisp( data_map('Feb').temperature )
Is there a better way to store this type of data, append to it afterwards, and still access it nicely by a key name?
Best Answer