Selecting more than one string value from a shapefile attribute

8 ビュー (過去 30 日間)
Jason Brown
Jason Brown 2019 年 7 月 2 日
回答済み: Satwik 2025 年 4 月 21 日
I am working with the built-in shapefile 'usastatehi.shp' to select a group of states and draw them using geoshow. I currently have the following code:
IL = shaperead('usastatehi.shp','UseGeoCoords',true,...
'Selector',{@(name) strcmpi(name,'Illinois'),'Name'});
IN = shaperead('usastatehi.shp','UseGeoCoords',true,...
'Selector',{@(name) strcmpi(name,'Indiana'),'Name'});
OH = shaperead('usastatehi.shp','UseGeoCoords',true,...
'Selector',{@(name) strcmpi(name,'Ohio'),'Name'});
Is it possible to combine this into one statement?

回答 (1 件)

Satwik
Satwik 2025 年 4 月 21 日
Yes, the selection of multiple states can be combined into a single call to shaperead by having the selector function check if the state name is a member of a cell array containing the desired names. This can be done as follows:
states = {'Illinois','Indiana','Ohio'};
selectedStates = shaperead('usastatehi.shp', 'UseGeoCoords', true, ...
'Selector', {@(name) any(strcmpi(name, states)), 'Name'});
This approach makes the script more efficient and scalable.
I hope it helps!

カテゴリ

Help Center および File ExchangeMap Display についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by