Selecting more than one string value from a shapefile attribute

18 views (last 30 days)
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?

Answers (1)

Satwik
Satwik on 21 Apr 2025
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!

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!