M-File Errors when extracting a NetCDF data

2 views (last 30 days)
So i was helped previusly to develop an m-file for extracting NetCDF data, i used the file previusly but is giving me erros now, so i am not what could be the problem. I get the following error.
Error using internal.matlab.imagesci.nc/openToRead (line 1299)
Unable to open file
"cams73_latest_ch4_conc_surface_satellite_dm_202101.nc".
Error in internal.matlab.imagesci.nc (line 124)
this.openToRead();
^^^^^^^^^^^^^^^^^
Error in ncdisp (line 71)
ncObj = internal.matlab.imagesci.nc(ncFile);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in CTPENC_Extraction (line 11)
ncdisp(filename);
^^^^^^^^^^^^^^^^
May you please help. I have attached the the m-file i am using. The NetCDF file is very large, i wanted to attach it as well.

Answers (1)

Mathieu NOE
Mathieu NOE on 6 Nov 2025
hello
I tested your code with another nc file I have on my pc and there seems to be no issue as soon as the m file and the nc files are in the same folder
if you store the nc files somewhere else in a separate folder then you must give the full path and pass it to the functions
so use fullfile in the data loading section, as you did for the excel export
clear all,clc
% Step 1: Specify longtitude and latitude of interest....
LG=[16.5 19.5 25.5 28.5 22.5 22.5];
LT=[-33 -33 -33 -33 -29 -31];
% Step 2: Display all variables in NetCDF file using dcsisplay command
[filename,PathName,FilterIndex] = uigetfile('*.nc');
filenameP = fullfile(PathName,filename);
ncdisp(filenameP);
% Step 3: Read variables from NetCDF file by command ncread
Meth_Levels = ncread(filenameP,'CH4');
% Size: 120x90x34x31
% Dimensions: longitude,latitude,level,time
long = ncread(filenameP,'longitude');
lat = ncread(filenameP,'latitude');
time = ncread(filenameP,'time');
level = ncread(filenameP,'level');
temp = ncread(filenameP, 'T');
hum = ncread(filenameP, 'Q');
altud = ncread(filenameP, 'altitude');
% Step 4: return data for the locations found....
for i=1:numel(LG)
LGG=find(long==LG(i));
LTT=find(lat==LT(i));
if isempty(LGG)||isempty(LTT)
disp('LOCATION NOT FOUND IN THIS NetCDF FILE')
break
end
% Step 5 :Convert 4 dimensional array to column vector array for one station
M_Levels(:,:,i) = squeeze(Meth_Levels(LGG,LTT,:,:)); % do not use the same variable name on the LHS and RHS
X(i)={'Methane_Data (mm/day)'};% label for variable
end
latt2=[LG ; LT];
%
time=double(time);
AA=time/24+datenum('2015-01-01 00:00:0.0');
[yy,mm,dd,~,~,~] = datevec(AA);
date1=[yy mm dd];
DD={'Year','Month','Day'};
% Exporting the returned data to excell
[FileName,PathName] = uiputfile('*.xlsx','Save file','.xlsx');
filename1 = fullfile(PathName, FileName);
sheet=1;
C={'LOCATION: Cape Town','','','Longitude';'Date:','','','Latitude'};
writecell(C,filename1,"Sheet",sheet,"Range",'A1');
writecell(DD,filename1,"Sheet",sheet,"Range",'A3');
writematrix(latt2,filename1,"Sheet",sheet,"Range",'E1');
writecell(X,filename1,"Sheet",sheet,"Range",'E3');
writematrix(date1,filename1,"Sheet",sheet,"Range",'A4');
writematrix(M_Levels,filename1,"Sheet",sheet,"Range",'E4');

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!