Selecting more than one string value from a shapefile attribute
18 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
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!
0 Comments
See Also
Categories
Find more on Map Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!