All values I am trying to extract from my NetCDF file results in NaN values

7 次查看(过去 30 天)
Hello!
I am new to using matlab and I am attempting to extract values from an ERA-5 Land NetCDF file. I sucessfully managed to do this online but now I have decided to use the latest downloaded version of Matlab and all my results are NaN values. Please may anyone advise me with how to sucessfully run this on my desktop?
The last two results from my command line:
AirTemp1(:,:,8760) =
NaN
Final_Table =
0×0 empty cell array
  2 个评论
MJFcoNaN
MJFcoNaN 2022-7-22
Hello,
Please post more codes about reading and dealing with your nc files. The result of ncdisp and ncinfo will help too.

请先登录,再进行评论。

回答(1 个)

Aditya
Aditya 2023-10-6
Hi Leeza,
From your query, I understand that you are facing an issue reading a NetCDF file in MATLAB. To assess this issue I have created a sample test file, and it appears to be working correctly.
Please find the code snippet below, which demonstrates the creation of a sample NetCDF file with random values and the subsequent reading of the data:
% Create sample data
lat = -90:10:90;
lon = -180:20:180;
time = datetime('2022-01-01'):hours(1):datetime('2022-01-02');
time_numeric = datenum(time); % Convert datetime to double
data = randn(length(lat), length(lon), length(time));
% Create NetCDF file
ncfile = 'sample_data.nc';
nccreate(ncfile, 'lat', 'Dimensions', {'lat', length(lat)});
nccreate(ncfile, 'lon', 'Dimensions', {'lon', length(lon)});
nccreate(ncfile, 'time', 'Dimensions', {'time', length(time)});
nccreate(ncfile, 'data', 'Dimensions', {'lat', length(lat), 'lon', length(lon), 'time', length(time)});
% Write data to NetCDF file
ncwrite(ncfile, 'lat', lat);
ncwrite(ncfile, 'lon', lon);
ncwrite(ncfile, 'time', time_numeric); % Use converted double data
ncwrite(ncfile, 'data', data);
% Read the latitude, longitude, time, and data variables
lat1 = ncread(ncfile, 'lat');
lon1 = ncread(ncfile, 'lon');
time1 = ncread(ncfile, 'time');
data1 = ncread(ncfile, 'data');
% Specify the indices for the desired data point
lat_index = 3; % Example: 3rd latitude index
lon_index = 5; % Example: 5th longitude index
time_index = 10; % Example: 10th time index
disp(size(data1))
% Access the data at the specified indices
value = data1(:,:, time_index);
% Display the value
disp(value);
For further guidance on reading or writing NetCDF files, please refer to this documentation:
If the issue persists, kindly provide the complete code and any additional files involved so that I can further assist you.
Hope this helps!

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by