creating new column using containers.Map, skip to next row if key doesn't exist??????
5 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Guillaume
2015-7-24
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;
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!