Containers.Map how to set keys and values
12 次查看(过去 30 天)
显示 更早的评论
Hello, so I want to create a Containers.Map. However, I am unsure of how to create it as the data that I want to use as key values is embedded in a cell array. For example, the cell array is 15x1 and looks like this: [1501x15 table, 1501x15 table....etc]I want to create a containers map that uses the 6th column of EACH table as the key values and the 8th column as the pairing values. How do I go about doing this?
0 个评论
采纳的回答
Sudhanshu Bhatt
2015-7-24
编辑:Sudhanshu Bhatt
2015-7-24
Hi Xander,
I am assuming that you already have a cell array which contains tables as cells.
For this example we will consider the following structure:
TableName T1 ; Columns: Age,Height,Weight,BloodPressure,LastName
TableName T2 ; Columns: Age,Height,Weight,BloodPressure,LastName
Step 1: Fetch a Table from the cell array :
>> grabATable = tableAsCells{1};
Step 2: Fetch columns from the table that you want to keep as keys:
>> keySets = table2cell(grabATable(:,5));
The function "table2cell" will convert the 5th column of the table elements into a cell array , which will act as our keys in the container.Map object.
Step 3: Fetch column from the table to be the values:
>> valueSets = table2cell(grabATable(:,3));
The function "table2array" will convert the 5th column of the table elements into a cell , which will act as our values in the container.Map object. NOTE: We could also use "table2array" function here.
Step 4: Put the keySets and valueSets in a container.Map object:
>> mapObj = containers.Map(keySets,valueSets);
Step 5: Fetching values form the map.
>> mapObj('Any_Key_From_keySets_cell_array');
For Example:
>> mapObj('Williams');
More information on "table2cell" , "table2array" and "containers.Map" can be found in the documentation links below:
Note: If you have to append in the map, if can be only done column wise.
I have attached a sample code to try this.
Hope this helps.
Thanks
Sudhanshu Bhatt
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dictionaries 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!