How to read shape file in matlab?

I am using following matlab code to read shape file. I am attaching the shape file also as zip file.
% pickup the shape files
d = uigetdir(pwd, 'Select a folder');
shapefiles = dir(fullfile(d, '*.shp'));
for n = 1:length(shapefiles)
\ shapefile = shapefiles(n);
disp(shapefile.name);
S = shaperead(shapefile.name);
\ polygon = polyshape([S.X], [S.Y]);
% Create a logical mask
logical_mask = inpolygon(lon, lat, polygon.Vertices(:, 1), polygon.Vertices(:, 2));
end
This is giving the following errors;
>> test\r\nAchi Khurd.shp
Error using openShapeFiles>checkSHP (line 82)
Unable to open file 'Achi Khurd.shp'. Check the path and filename or file permissions.
Error in openShapeFiles (line 19)
[basename, ext] = checkSHP(basename,shapeExtensionProvided);
Error in shaperead (line 212)
= openShapeFiles(filename,'shaperead');
Error
in test (line 9)
S = shaperead(shapefile.name);
>>
Please suggest me how to fix it? I would be highly obliged for kind help.
Dave

8 Comments

Please share the data for the variables lat and lon as well, so that we can run your code.
How is the file located in your PC? Are you trying to access it from a compressed folder or a regular folder? Does MATLAB have access to the folder in question?
Shape file itself contains lat and lon.
access it from a regular folder
Yes MATLAB have access to the folder in question
I hope it will be helpful.
Dave
Hi,
Can you first try to remove spaces in your file name and file extension.
For example: Achi Kurd.shp --> Achi_Kurd.shp
Please do this to the folder too. Mostly matlab dosent like spaces.
Then there are few other steps to consider like:
  1. It should be on the same directory as your code.
  2. Check if your shape file have pubic access provided to it.
If you have provided the shape file in a different path and if it is absolutely necessary you can provide the fulle path of the file like below
shapefilePath = 'C:\path\to\your\shapefile\Achi_Khurd.shp';
S = shaperead(shapefilePath);
Hope this should do the trick.
"Mostly matlab dosent like spaces."
Spaces in file names and paths are fine.
fn = 'a file name with spaces.txt';
writematrix(magic(5),fn);
type(fn) % works
17,24,1,8,15 23,5,7,14,16 4,6,13,20,22 10,12,19,21,3 11,18,25,2,9
type('a file name with spaces.txt') % works
17,24,1,8,15 23,5,7,14,16 4,6,13,20,22 10,12,19,21,3 11,18,25,2,9
But you might run into problems if you use command syntax.
type 'a file name with spaces.txt' % works
17,24,1,8,15 23,5,7,14,16 4,6,13,20,22 10,12,19,21,3 11,18,25,2,9
try
type "a file name with spaces.txt" % error
catch e
disp(e.message);
end
File '"a' not found.
try
type a file name with spaces.txt % error
catch e
disp(e.message);
end
File 'a' not found.
Thank you @Voss for the clarification
Any updates, @Devendra? Did you check @Voss's answer?

(Answers Dev) Restored edit

(Answers Dev) Restored edit

Answers (1)

Voss
Voss on 26 Mar 2024
Edited: Voss on 26 Mar 2024
You are attempting to read a file in the current directory:
S = shaperead(shapefile.name);
That is, you are not taking into account the location of that file.
You should specify an absolute or relative path to the file, e.g.:
file_name = fullfile(shapefile.folder,shapefile.name);
S = shaperead(file_name);
d = uigetdir(pwd, 'Select a folder');
assert(~isnumeric(d),'No folder selected')
shapefiles = dir(fullfile(d, '*.shp'));
for n = 1:length(shapefiles)
shapefile = shapefiles(n);
file_name = fullfile(shapefile.folder,shapefile.name);
disp(file_name);
S = shaperead(file_name);
polygon = polyshape([S.X], [S.Y]);
% Create a logical mask
logical_mask = inpolygon(lon, lat, polygon.Vertices(:, 1), polygon.Vertices(:, 2));
% ...
end

This question is locked.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2024a

Asked:

on 26 Mar 2024

Locked:

on 5 Jun 2024

Community Treasure Hunt

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

Start Hunting!