MATLAB Answers


How to organize data in matrix

Asked by FELIPE PIMENTEL on 1 Oct 2019
Latest activity Commented on by FELIPE PIMENTEL on 3 Oct 2019
Hi, I have a dataset that is in netCDF format. These data are precipitation and have 5 variables. I reduced the 2D I need (lat, lon) and now I need to save to a 3D cell with fixed lat and lon and time varying. I am unable to save so that they are not subscribed to previous data. Any help will be appreciated.


Show 1 older comment
Upload a sample netcdf file if you can or link to one.
Or do a ncdisp(file) and post the output.
Sorry, but this is gibberish so far as you have written. You may understand what you are asking to do, but it makes no sense at all.
So give a SMALL, SIMPLE example, only a few numbers. Then show what you would hope to find as a result.
There is no need to even post a netcdf file, as that just means you have not read it into MATLAB yet. It is just a bunch of numbers.
If you want help, then make it easy for somone to help you.
FELIPE PIMENTEL's "Answer" moved here:
Sorry it was not clear.
I have files in netCDF format with the following variables:
S = 1
M = 10
Y = 181
X = 360
L = 7
Size: 1x1
Dimensions: S
Datatype: double
units = 'months since 1982-01-01'
calendar = 'standard'
axis = 'N'
Size: 10x1
Dimensions: M
Datatype: single
units = 'Unitless'
long_name = 'Ensemble Member'
axis = 'M'
Size: 181x1
Dimensions: Y
Datatype: single
units = 'degrees_north'
long_name = 'latitude'
axis = 'Y'
Size: 360x1
Dimensions: X
Datatype: single
units = 'degrees_east'
long_name = 'longitude'
axis = 'X'
Size: 7x1
Dimensions: L
Datatype: single
units = 'months'
long_name = 'Lead'
standard_name = 'forecast_period'
axis = 'L'
Size: 360x181x7x10x1
Dimensions: X,Y,L,M,S
Datatype: double
units = 'mm'
long_name = 'Precipitation'
missing_value = -999
fill_value = -999
For "M", I have a mean and for "L" I select 1, 2 or 3 as needed. I need to open these files one by one, select lat and lon for South America and write in a continuous matrix with monthly data from 1982/jan to 2010/dec.
That is, I need an organized 3D matrix like lat, lon and monthly interval precipitation. That way my matrix would be 60x45x348 (lat x lon x time) All the part of selecting and extracting the data I am doing. The problem is saving these files to a new array because I'm overwriting over the previously read data and the array ends up as 2D with the last read data.

Sign in to comment.




1 Answer

Answer by meghannmarie on 3 Oct 2019

Did you preallocate output array?
output_array = nan(60,45,348);
for time_idx = 1:348
output_array(:,:,time_idx) = ncread(file,'pr',start,count);

  1 Comment

Thank you very much.
That was exactly what I was looking for.

Sign in to comment.