Borrar filtros
Borrar filtros

Using Containers.Map to find unique rows in a text file

2 visualizaciones (últimos 30 días)
Zachary Messaglia
Zachary Messaglia el 4 de Mayo de 2018
Comentada: Walter Roberson el 7 de Mayo de 2018
I am creating a script or function that will be able to take a text file with 2 collums (Firstname, Lastname) and about 900 rows. Below is my script so far, I made sure to add plenty of notes! The purpose of this script or function is to be able to take two different lists of peoples names from an excel file where their last names and first names are in separate columns and paste that into a text file. I am having difficulty of knowing where to go from here to make this work. I am also wondering if I should make this a script or a function in order to properly read the text files with name lists. The output of this code should be names that are only displayed once. So if the name John Doe is only in the text file once then John Doe will be included in the output while if the name Sam Kemper had his name listed twice then he would not be in the output.
My code:
MATLAB code
clear;
clc;
%
m = memmapfile('lcnames.txt.')
%places the text file into the memory of faster reading of the names, just
%a fun part to add to the script, not entirely nessisary
%
nameMap = containers.Map;
%not entirely sure what this does
%
%Suppose you're going though a file
nextName = 'John Doe';
considerName( nextName, nameMap );
nextName = 'George Doe';
considerName( nextName, nameMap );
nextName = 'John Doe';
considerName( nextName, nameMap );
nextName = 'John Smith';
considerName( nextName, nameMap );
nextName = 'John Smith';
considerName( nextName, nameMap );
%This stuff here is what it'll be like
%
keys = nameMap.keys;
values = nameMap.values;
size = nameMap.size;
%think of these three as like a three dimensional map they each relate to
%eachother
%
for i = nameMap.keys
if( nameMap(char(i)) == 1 )
%This person was unique
disp( i );
%This part of the script is to show people whose name is not on the
%list twice
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 5 de Mayo de 2018
t = readtable('YourFile.txt', 'VariableNames', {'Firstname', 'Lastname'}, 'ReadVariableNames', false);
[G, TID] = findgroups(t);
counts = accumarray(G);
only_occurs_once = TID(counts==1);
  2 comentarios
Zachary Messaglia
Zachary Messaglia el 7 de Mayo de 2018
Thank you for submitting this, can you please elaborate more on the code you commented? I read over it and used the wiki to help understand the parts I didn't understand. I still can't seem how to get the code to work but what you commented makes sense, especially the only_occurs_once line
Walter Roberson
Walter Roberson el 7 de Mayo de 2018
Do you have a small sample input file?

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by