Using a containers.Map to find values

6 views (last 30 days)
Hello, so I have a 35x1 cell array that contains a table of data for every day. The cell array looks like "[1501x6 table, 1501x6 table....ETC]". In addition, I also have a containers.map based off of a 12000x2 excel pull in. My objective is to insert a 7th column in every table (or cell of the cell array), in which every cell of the table corresponds to the value assigned in the map (using the 4th column as the keys). How do I do this? I've used a for loop to no avail. My idea for code was:
For I:numel(data) data{I}=[data(:,6) values{i}(map,{data(:,4)}] end

Accepted Answer

Sudhanshu Bhatt
Sudhanshu Bhatt on 24 Jul 2015
Edited: Sudhanshu Bhatt on 24 Jul 2015
Hi Xander,
I am assuming that you have the data you want to insert into the table as 7th column.
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: (Table T1); (Table T2)
NewColumn Smoker: {'Yes';'Yes';'Yes';'No';'No'};
There are two approaches to adding a column to an existing table as listed below:
Approach 1: Appending the new column to the pre-existing table:
Step 1: Fetch a Table from the cell array :
>> grabATable = tableAsCells{1};
Step 2: Add column to this table :
>> T1 = [T1, Smoker];
>> T1.Properties.VariableNames(6) = {'Smoker'};
Approach 2: Creating a temporary table and concatenating it to the original table :
Step 1: Fetch a Table from the cell array :
>> grabATable = tableAsCells{1};
Step 2: Add column to this table :
>> newTable = table(Smoker);
>> T1 = [T1,newTable];
Below are the steps to add key and value objects to the Existing container.Map object:
Step 1: Creating new key and Value pairs :
>> newKeyValueSets = {'PlaceHolder1', 'PlaceHolder2', 'PlaceHodler3', 'PlaceHoler4' };
>> newValueSets = {100,110,120,130};
Step 2: Creating a new Map object :
>> newMapObj = containers.Map(newKeyValueSets,newValueSets);
Step 3: Concatenate the two map objects into the original object :
>> mapObj = [mapObj; newMapObj];
More information on concatenating values to map objects can be found in the documentation link below:
Hope this helps.
Thanks
Sudhanshu Bhatt

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!