How to deal with time in translating netcdf files to csv

7 views (last 30 days)
Hello,
I have a netcdf file which I am trying to unwrap. I have lat, lon, tmax, and time variables. I want to transform this data into a 2D matrix to print it to csv. I expect the matrix to have 87x97x31 rows and 4 columns(lon lat tmax time).
whos lat lon tmax time
Name Size Bytes Class Attributes
lat 87x1 696 double
lon 97x1 776 double
time 31x1 248 double
tmax 97x87x31 2092872 double
For now, I use the following code to get a matrix with 87x97x31 rows and 3 columns. But I don't know how to add a fourth column with the time dimension to the matrix. Please could you help me? I just need to know how to create the column with the time information. I know how to transform the raw time format into readable time using the dateime function.Thank you.
tmax=ncread(filepath,'tmax',startloc,count);
lon=ncread(filepath,'lon', startloc(1),count(1));
lat=ncread(filepath,'lat', startloc(2),count(2));
time=ncread(filepath,'time');
%Generate matrix
data=[];
j=1;
for k=1:size(lon)
for m=1:size(lat)
data(j,1)=lon(k,1);
data(j,2)=lat(m,1);
for h=1:size(tmax,3)
data(j,h+2)=tmax(k,m,h);
end
j=j+1;
end
end
%%%% Reshape the data to have only one temperature variable, and not the number of days
temp=[];
temp=array2table(data);
data=stack(temp,3:width(data));

Answers (1)

Arjun
Arjun on 3 Jun 2025
I understand that you have data corresponding to latitude, longitude, time and temperature with different dimensions making it difficult to unwrap. You intend to end up with a 2-D matrix (87x97x31) rows and 4 columns.
In order to unpack your data you can use "ndgrid" function of MATLAB to replicate latitude, longitude and time data in 3-D space and then convert them into respective column vectors this will ensure that each column will have 87x97x31 rows. Similarly temperature data 'tmax' also has to be flattened into a column vector. Once you have all the column vectors with same number of rows you can stack them together to get the final matrix with 4 columns and (87x97x31) rows.
Kindly refer to the code section below:
% Load the data correctly
tmax=ncread(filepath,'tmax',startloc,count);
lon=ncread(filepath,'lon', startloc(1),count(1));
lat=ncread(filepath,'lat', startloc(2),count(2));
time=ncread(filepath,'time');
% Create 3-D for lon, lat, and time using ndgrid function
[Lon, Lat, Time] = ndgrid(lon, lat, time); % all are 87x97x31
% Reshape all to column vectors
Lon = Lon(:);
Lat = Lat(:);
Time = Time(:);
% Reshape tmax also to column vector
Tmax = tmax(:);
% Combine into one matrix
data = [Lon, Lat, Tmax, Time];
Once you have this matrix then you can use 'datetime' function on the time values and use 'writetable' function to write this matrix to a ".csv" file.
Refer to the documentation of 'ndgrid' here: https://www.mathworks.com/help/matlab/ref/ndgrid.html
I hope this helps!

Community Treasure Hunt

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

Start Hunting!