📄 city2lonlat.m
字号:
function XY = city2lonlat(name,st,city)
%CITY2LONLAT Select longitude and latitude of city.
% XY = city2lonlat(name,st)
% = city2lonlat(name,st,city)
% s = city2lonlat(...), select city in dialog if multiple 'name' or
% 'st' matches and return stucture 's' of city
% data if multiple cities selected in dialog
% name = city name string or cell array of multiple strings
% = [], all names
% st = 2-char state abbreviation string or, if multiple values, cell
% array of strings
% = [], all states
% city = (optional) stucture with fields:
% .Name = m-element cell array of m city name strings
% .ST = m-element cell array of m 2-char state abbreviations
% .XY = m x 2 matrix of city lon-lat (in decimal deg)
% .Pop = m-element vector of city population estimates (2000)
% = USCITY10K, default (which contains all cities in US with
% population of at least 10,000)
% XY = 1 x 2 row vector of city 'name','st' lon-lat (in decimal,
% degrees) if single state and multiple city names, or equal
% number of citynames and states
% s = stucture of city data, if multiple cities selected in dialog
%
% Examples:
% xy = city2lonlat('Raleigh','NC') % xy = -78.6446 35.8188
%
% XY = city2lonlat({'Raleigh','Gainesville'},{'NC','FL'})
% % XY = -78.6446 35.8188
% % -82.3361 29.6652
%
% XY = city2lonlat([],'NC') % Dialog to select cities in North
% % Carolina with > 10,000 population
%
% XY = city2lonlat([],'NC',uscity50k) % Dialog to select cities in North
% % Carolina with > 50,000 population
%
% See also LONLAT2CITY
% Copyright (c) 1994-2006 by Michael G. Kay
% Matlog Version 9 13-Jan-2006 (http://www.ie.ncsu.edu/kay/matlog)
% Input Error Checking ****************************************************
if nargin < 1, name = []; end
if nargin < 2, st = []; end
if ~isempty(name) && ~ischar(name) && ~iscell(name)
error('Argument "name" must be a string or cell array.')
elseif ~isempty(st) && ~ischar(st) && ~iscell(st)
error('Argument "st" must be a string or cell array.')
end
if ischar(name), name = {name}; end
if ischar(st), st = cellstr(repmat(st,length(name),1)); end
if nargin < 3
city = uscity10k;
else
if ~isstruct(city)
error('"city" must be a structure.')
elseif ~all(ismember({'Name','ST','XY','Pop'},fieldnames(city)))
error('Fields in structure "city" not correct.')
elseif ~iscell(city.Name)
error('"city.Name" must be a cell array.')
end
m = length(city.Name(:));
if ~iscell(city.ST) || length(city.ST) ~= m
error('"city.ST'' must be an m-element cell array.')
elseif ~isnumeric(city.XY) || ~isequal(size(city.XY),[m 2])
error('"city.XY" must be an m x 2 matrix of real numbers.')
elseif ~isnumeric(city.Pop) || length(city.Pop) ~= m
error('"city.Pop" must be an m-element vector of real numbers.')
end
end
% End (Input Error Checking) **********************************************
m = length(city.Name(:));
namedat = city.Name;
stdat = city.ST;
if ~isempty(name) && ~isempty(st) && length(name) == length(st)
idx = [];
for i = 1:length(name)
idxi = intersect(strmatch(lower(name{i}),lower(namedat)), ...
strmatch(upper(st{i}),stdat));
if length(idxi) < 1
error([name{i} ', ' st{i} ' not found.'])
elseif length(idxi) > 1
error(['Multiple ' name{i} ', ' st{i} ' found in data.'])
else
idx = [idx; idxi];
end
end
else
if isempty(name)
idxname = (1:m)';
else
idxname = [];
for i = 1:length(name)
idxname = [idxname; strmatch(lower(name{i}),lower(namedat))];
end
end
if isempty(st)
idxst = (1:m)';
else
idxst = [];
for i = 1:length(st(:))
idxst = [idxst; strmatch(upper(st{i}),stdat)];
end
end
[idx,ia,ib] = intersect(idxname,idxst);
if isempty(name) && ~isempty(st) % Keep in same order as input
idx = idx(invperm(ib));
elseif isempty(st) && ~isempty(name)
idx = idx(invperm(ia));
end
end
if isempty(idx)
XY = []; idx = [];
elseif length(idx) == 1 || (length(idx) == length(name) && ...
(length(idx) == length(st(:)) || length(st(:)) == 1))
XY = city.XY(idx,:);
else
i = listdlg('ListString',strcat(city.Name(idx),',',city.ST(idx)),...
'Name','US Cities');
if isempty(i)
XY = [];
idx = i;
elseif length(i) == 1
XY = city.XY(idx(i),:);
idx = idx(i);
else
XY.Name = city.Name(idx(i));
XY.ST = city.ST(idx(i));
XY.XY = city.XY(idx(i),:);
XY.Pop = city.Pop(idx(i));
idx = idx(i);
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -