why am i getting this error ? what to do?

3 views (last 30 days)
dirpath=('C:\Users\AKASH\Pictures\works\**');
files=dir('C:\Users\AKASH\Pictures\works\**\3B*');
nfiles=length(files);
datadir = 'C:\Users\AKASH\Pictures\works\**';
fname = '3B42.19980101.00.7.SUB.nc';
lat1 = ncread([datadir,fname],'latitude');
lon1 = ncread([datadir,fname],'longitude');
Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open C:\Users\AKASH\Pictures\works\**3B42.19980101.00.7.SUB.nc for reading.
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);

Accepted Answer

JAYANTH BHIMAVARAPU
JAYANTH BHIMAVARAPU on 21 Jun 2018
Edited: JAYANTH BHIMAVARAPU on 21 Jun 2018
Replace datadir = 'C:\Users\AKASH\Pictures\works\**'; with datadir = 'C:\Users\AKASH\Pictures\works\**\'; and try again. But don't forget to change * * with exact file location path.
  2 Comments
Stephen23
Stephen23 on 21 Jun 2018
@JAYANTH BHIMAVARAPU: how will that help if that directory still does not exist, and so concatenting it (with or without the file separator) will always produce an error?
JAYANTH BHIMAVARAPU
JAYANTH BHIMAVARAPU on 21 Jun 2018
Yeah I understood your point but I mean to say that if he gives the correct path to that .nc file with file separators, he can access that file for reading.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 21 Jun 2018
Edited: Stephen23 on 21 Jun 2018
Read the error message. Is datadir = 'C:\Users\AKASH\Pictures\works\**' a directory that exists on your computer? Is C:\Users\AKASH\Pictures\works\**3B42.19980101.00.7.SUB.nc the full name of a file on your computer? Neither that path nor that file exist on your computer. You can see these names clearly in the error message and your code, so it is easy to see why there is an error.
Also, use fullfile rather than string concatenation. If you don't need to use the recursive search, then you will need to do something like this:
D = 'C:\Users\AKASH\Pictures\works';
S = dir(fullfile(D,'3B*.nc'));
for k = 1:numel(S)
fnm = fullfile(D,S(k).name);
lat = ncread(fnm,'latitude');
lon = ncread(fnm,'longitude');
...
end

Categories

Find more on Encryption / Cryptography 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!