Containers.Map how to set keys and values

12 ビュー (過去 30 日間)
xander fong
xander fong 2015 年 7 月 22 日
コメント済み: xander fong 2015 年 7 月 24 日
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?

採用された回答

Sudhanshu Bhatt
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
CellArray tableAsCells; Rows: T1; T2
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 ExchangeDictionaries についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by