0

I have a .csv file where multiple postcodes (characters and numbers) correspond to a unique ID number (also characters and numbers).

e.g

BS2 9TL, E00073143
BS2 9TB, E00073143
BS2 9XJ, E00073143
BS2 8AT, E00073144
BS2 8TY, E00073144
BS2 8UA, E00073144
BS2 8UG, E00073144

I need to create a new array for each unique ID number that stores the respective postcodes. The amount of postcodes for each ID number is not the same every time.

The file contains 9010 postcodes and 1258 ID numbers.

Can anyone show me how to go about doing this?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
scadda
  • 31
  • 1
  • 1
  • 5

2 Answers2

0
PCs=importdata('PostalCodes.csv'); %// import data
PostalCodes = cell(numel(PCs,2)); %// create storage
IDs = cell(numel(PCs,2));
for ii = 1:numel(PCs)
    tmp = strsplit(PCs{ii,1}, ','); %// split on comma
    PostalCodes{ii,1} = tmp{1};
    IDs{ii,1} = tmp{2};
end

[IDs,idx] = sort(IDs); %// sort on ID
PostalCodes = PostalCodes(idx); %// sort PCs the same way
PostalCodes = cell2mat(PostalCodes); %// go to matrix

[IdNums,~,tmp2] = unique(IDs); %// get unique IDs
tmp3 = [1; find(diff(tmp2)); numel(IDs)]; %// create index array

for ii = 1:numel(tmp3)-1;
    PostalCode(ii).IDs = PostalCodes(tmp3(ii):tmp3(ii+1),:); %// store in struct
end

You don't actually want separate arrays, because that's very bad practise, so I've put everything in a structure for you. You can now access the structure by simply typing:

PostalCode(1).IDs(2,:)
ans =
BS2 9TL

where the (1) beteen PostalCode and IDs corresponds to the ID (which is found in IdNums), and the (2,:) plucks out the second postal code corresponding to ID IdNums(1).

Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
0

You could use an array of structs

[x,y]=textread('/tmp/file.csv' , '%s %s','delimiter',',')

csv=[x,y]


values=struct('key',{},'value',{})

keys= unique(csv(:,2));
for i = 1:length(keys)
  values(i).key=keys{i}
  values(i).value=csv( strcmp( csv(:,2) , keys{i}),1)
end

Tested this using octave. On matlab you could use a map container instead of key/value structs for direct access via id's

xvan
  • 4,554
  • 1
  • 22
  • 37