MATLAB Answers

How to extract values from a 3-d .mat file?

4 views (last 30 days)
Keegan Carvalho
Keegan Carvalho on 20 Mar 2020
Edited: Keegan Carvalho on 22 Mar 2020
Hi,
I loaded a netcdf file and derived a .mat file which is a 3-D matrix of lon x lat x value (360 x 40 x 111). I wanted to extract the third dimension as entire column.
lon=ncread('sstarc.nc','lon');
lat=ncread('sstarc.nc','lat');
summean=ncread('sstarc.nc','sst');
I used the above code and got the mat file. The third dimension has 111 values which I wanted to extract the "111" values for the entire 360 x 40 grid as a column (eg. time series). Attached .mat file!
Looing forward to your help. Thanks!

  0 Comments

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 21 Mar 2020
Your mat file contains variables. Loading the mat file loads the variables back into the workspace.
load summean.mat
If there are multiple variables stored in the mat file, you can tell it to load specific variables instead of all of them.
load summean.mat filteredA
As for the values to extract, you can do that using indexing on the variable containing the values.
The third dimension does not have 111 values. Instead, you have 111 matrices of dimension 360 x 40 stacked on top of each other. To extract the last one, you could do the following
vals = filteredA(:,:,111);

  9 Comments

Show 6 older comments
Cris LaPierre
Cris LaPierre on 22 Mar 2020
The variable filteredA contains the data for Jun-Aug for all 37 years (512 x 50 x 111). Now you want this data as a time series. What does that mean to you? Knowing there are now 2,841,600 values, how do you envision this data being organized in a vector? Do you want to preserve location information? Month? Year?
I would be inclined to create a timetable, thus preseving the month and year info. I'd add variables (columns) for sst value, lon and lat.
Following your example above, this is what the code might look like.
% extract data from nc file
lon=ncread('sst.nc','longitude');
lat=ncread('sst.nc','latitude')';
sst=ncread('sst.nc','variable');
% identify summer months
startDate = datetime(1982,1,1,"Format","MMM-uuuu");
endDate = datetime(2018,12,31,"Format","MMM-uuuu");
dates = startDate:calmonths(1):endDate;
summerMonth = month(dates) >= 6 & month(dates) <= 8;
% Set up variables for creating timetable
datesTT = permute(repmat(dates,512,1,50),[1,3,2]); % create a date for each value
datesTT = datesTT(:,:,summerMonth);
[LAT, LON] = meshgrid(lat,lon);
latTT = repmat(LAT,1,1,sum(summerMonth)); % create matrix same size as datesTT
lonTT = repmat(LON,1,1,sum(summerMonth)); % create matrix same size as datesTT
sstSummer = sst(:,:,summerMonth); % Extract data for summer months
% Create timeTable
summerTT = timetable(datesTT(:),sstSummer(:),lonTT(:),latTT(:));
summerTT.Properties.VariableNames = {'sst','lon','lat'};
Here's a preview of what the table looks like (first 8 rows).
Time sst lon lat
________ ______ _______ ______
Jun-1982 271.46 0 89.463
Jun-1982 271.46 0.70313 89.463
Jun-1982 271.46 1.4063 89.463
Jun-1982 271.46 2.1094 89.463
Jun-1982 271.46 2.8125 89.463
Jun-1982 271.46 3.5156 89.463
Jun-1982 271.46 4.2188 89.463
Jun-1982 271.46 4.9219 89.463
Cris LaPierre
Cris LaPierre on 22 Mar 2020
As for taking the mean when there are NaN entries, you can use the following syntax
mean(A,'omitnan')
You can see an example here.
Keegan Carvalho
Keegan Carvalho on 22 Mar 2020
Thank you Cris. Very grateful for the explanations. I actually have another query on the same. I am trying to create a map of seasonal means and thereby want to have 37 years of data (360 x 40 x37). I used the following code:
lon=ncread('sstarc.nc','lon');
lat=ncread('sstarc.nc','lat');
sst=ncread('sstarc.nc','sst');
DJF = sort([[1:12:444],[2:12:444],[12:12:444]]);
Wacc = cumsum(sst(:,:,DJF));
Wavg = Wacc(:,:,3:3:end)/3;
meanDJF=mean(Wavg,3);
h=pcolor(lon,lat,meanDJF);
However I seem to get odd temperature values of 100 degrees and above at certain places. I asked this question: (https://se.mathworks.com/matlabcentral/answers/512283-how-to-extract-seasonal-means)
I know this may seem to be going too far, but I would gladly appreciate your help as I am working on a project.

Sign in to comment.

Sign in to answer this question.


Translated by