Reputation: 267
In MATLAB R2016a, I am reading a table and creating a map from its two columns, Fruit (TEXT) and Calories (NUMBER):
map_table = readtable('map.csv');
Keys = table2cell(map_table(:, 1)); %Fruits
Values = table2cell(map_table(:, 2)); %Calories
Map = containers.Map(Keys, Values);
I also have another table with a column called Food (TEXT) containing all of the values of the Fruit column multiple times:
table = readtable('table.csv');
I want to create another column in table with the calories for every food in table.Food. This is accomplished by mapping the values in table.Food to the calories using Map.
I have tried the below but get an error:
Map(table.Food)
Error using containers.Map/subsref
Specified key type does not match the type expected for this container.
When I pass a single key in the map it does return a number:
Map('Apple')
ans = 90
How can I accomplish the mapping? I know in R, there is a function called mapvalues which accomplishes this.
Upvotes: 0
Views: 117
Reputation: 202
You can't pass Map a cell array, so try a loop or using cellfun:
cellfun(@(x) M(x), table.Food)
Upvotes: 2