creating new column using containers.Map, skip to next row if key doesn't exist??????

14 views (last 30 days)
xander fong
xander fong on 24 Jul 2015
Answered: Guillaume on 24 Jul 2015
Hello, so I have a containers.Map that i am trying to use to produce a second column in a 1501x1 table called "x" and the column is TL2. I've tried using the code: >> x.col2=values(map,x.TL2);. However, MATLAB returns "Error using containers.Map/values The specified key is not present in this container". After running a ISKEY command, I've found that certain rows do not contain keys. How do I run this same function, but have matlab ignore/skip over the rows that do not have keys and fill in those that do, rather than simply returning this error?

Answers (1)

Guillaume
Guillaume on 24 Jul 2015
You're mixing up table and map vocabulary in your post, so it's a bit unclear. Maps don't have rows, tables don't have keys, so you can't have rows containing keys.
What I think you're saying is that you're using the values of column TL2 as keys into the map, and some of these TL2 values are actually not present in the key set of the map. You can't just ignore/skip over these missing keys though, you still need to put something in the corresponding row of the table.
You haven't specified what the content of the map is (= the type of column col2) so I'll assume a numerical value and will put NaN for those rows that are not part of the keyset of the map.
newcol = nan(height(x), 1); %temporary destination of values of the map. nan by default
newcol(isKey(map, x.TL2)) = values(map, x.TL2(iskey(map, x.TL2)));
x.col2 = newcol;

Community Treasure Hunt

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

Start Hunting!