% This script is an example to show how to read csv with string-type field
clear all;clc
% read data
fid = fopen('住宅区.csv');
title = textscan(fid, '%s %s %s %s %s %s',1,'delimiter',','); % format, rowcount, delimiter
data = textscan(fid,'%f %f %f %f %s %s','delimiter',','); % since cursor is one-direction, no need [rowcount]
fclose(fid);
lat1 = data{1,1}; lng1 = data{1,2};
lat2 = data{1,3}; lng2 = data{1,4};
type = data{1,5}; name = data{1,6};
% new cell/ list, 'R' for Record
% record = {};
% typeR = {}; NameR = {};
% lat1R = []; lng1R = []; lat2R = []; lng2R = [];
% initialize with zeros, then 'erase' zeros. It's faster than when increasing
% the size of arrray with loop, or u might consider using 'list(end+1) = '
% to append new element
num = size(cell2mat(data(1,1)),1);
lat1R = zeros(num,1); lng1R = lat1R; lat2R = lat1R; lng2R = lat1R;
NameR = cell(num,1); typeR = cell(num,1);
lat1R(1) = lat1(1); lng1R(1) = lng1(1); lat2R(1) = lat2(1); lng2R(1) = lng2(1);
NameR(1)=name(1); typeR(1) = type(1);
% consider the rule of naming communities
corpus = ['院' '村' '园' '苑' '阁' '庭' '轩' '居' '台' '湾' '区' '小区' '坊' ];
% start comparing
seq = 2;
tic
for i = 2 : num
tcName = cell2mat(name(i-1));
tcName2 = cell2mat(name(i));
pos = [];
for j = 1: size(corpus,2)
if (strfind(tcName2,corpus(j)))
pos = strfind(tcName2,corpus(j));
break;
end
end
if ~isempty(pos)
if isempty(strfind(tcName,tcName2(1:pos))) % Compare
lat1R(seq)=lat1(i);lng1R(seq)=lng1(i);
lat2R(seq)=lat2(i);lng2R(seq)=lng2(i);
NameR(seq)=name(i); typeR(seq)=type(i);
seq = seq +1;
end
else
if size(tcName2,2)>=3
if isempty(strfind(tcName,tcName2(1:3))) % Compare the first 5 letter
lat1R(seq)=lat1(i);lng1R(seq)=lng1(i);
lat2R(seq)=lat2(i);lng2R(seq)=lng2(i);
NameR(seq)=name(i); typeR(seq)=type(i);
seq = seq +1;
elseif isempty(strfind(tcName,tcName2(1:4))) % Compare the first 4 letter
lat1R(seq)=lat1(i);lng1R(seq)=lng1(i);
lat2R(seq)=lat2(i);lng2R(seq)=lng2(i);
NameR(seq)=name(i); typeR(seq)=type(i);
seq = seq +1;
elseif isempty(strfind(tcName,tcName2(1:3))) % Compare the first 4 letter
lat1R(seq)=lat1(i);lng1R(seq)=lng1(i);
lat2R(seq)=lat2(i);lng2R(seq)=lng2(i);
NameR(seq)=name(i); typeR(seq)=type(i);
seq = seq +1;
end
end
end
end
toc % 1.9s
% 'op' for output
opLat1 = lat1R(find(lat1R~=0));
opSize = size(opLat1,1);
opLat2 = lat2R(1:opSize);
opLng1 = lng1R(1:opSize);
opLng2 = lng2R(1:opSize);
opType = typeR(1:opSize);
opName = NameR(1:opSize); % 0.018s
% NameR(cellfun(@isempty,NameR))=[]; 0.023s
demand = ones(opSize,1);
% u might also want to find the seq. of empty ele. in cell
% b = cellfun('isempty',type);
% c = find(b==1);
% output to .csv 0.56s
datacolumns = {'lat_org' 'lng_org' 'lat_wgs' 'lng_wgs' 'type' 'name', 'capacity'};
data = table(opLat1,opLat2,opLng1,opLng2, opType, opName, demand);
writetable(data, 'residents_new.csv')