How to get data from containers.Map and store to a new table then view the table
14 次查看(过去 30 天)
显示 更早的评论
Hi there,
I have a code that reads an excel file, choose specific columns to view then deletes rows that contains NaN cell. Now I have a for loop and I have to use containers.Map so I can filter out the duplications of the data.
I understand that I can acces specific data in my map. but my problem now is that, I want to store the unique key and its value set to a new table. Then I want to be able to view the table with non duplicated data. After this I want to access that table to plot graphs. This is the code that I have at the moment.
%--FIND NaN ROWS THEN DELETE ENTIRE ROW
%imports data (.csv) file
PCBAllDataSet = readtable('testfile5.csv');
%imports the only columns needed
PCBRequiredCol = PCBAllDataSet(:,{'PcbTestID','PcbID','Verify12VInput','VerifyCurrentDraw'});
%remove row that contains NaN value
PCBRemoveNaNRow = rmmissing(PCBRequiredCol);
%--REMOVE (PCBID) ROW DUPLICATIONS
%create another table for keyset and valueset
%duplications will be removed as the table is made
newTable = containers.Map('KeyType','char','ValueType','any');
%forloop to assign key and value set on every row
for i = 1:size(PCBRemoveNaNRow,1)
%stores PCBID Column as key
%char(table2cell) -> type table cast it to cell then cast again to match KeyType'char'
keyID = char(table2cell(PCBRemoveNaNRow(i,2)));
%current(i) row, 3rd column to end column
valueSet = PCBRemoveNaNRow(i,3:end);
%create new table to assign every key to a value set
newTable(keyID) = valueSet;
end
Can anyone please help me how to extract these unique and value set to a new table so I can view it as a table then access the table to do graphs? Please Help :(
2 个评论
Peter Perkins
2019-1-23
It's not at all clear why you are using Map, and that's a lot of code to expect people to try to understand. If all you want to do is delete duplicate rows of a table, I suggest looking at the unique function.
回答(1 个)
Guillaume
2019-1-23
I'm not sure Andrea is still interested in an answer as the question is fairly old now. As Peter said, if the purpose of the conversion to a map is purely to remove unique element, then it's a complete waste of time. Simply use unique:
[~, rowstokeep] = unique(PCBRemoveNaNRow.PcbID); %possibly with the'stable' option
newTable = PCBRemoveNaNRow(rowstokeep, :)
%all done
Even the way the map is built is inefficient. There's no need for a loop:
map = containers.Map(PCBRemoveNaNRow.PcbID, mat2cell(PCBRemoveNaNRow, ones(1, height(PCBRemoveNaNRow)), width(PCBRemoveNaNRow)))
Converting that to a table can be done fairly simply:
values = map.Values;
newTable = vertcat(values{:})
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!