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

조회 수: 6 (최근 30일)
Keegan Carvalho
Keegan Carvalho 2020년 3월 20일
편집: Keegan Carvalho 2020년 3월 22일
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!

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 3월 21일
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
Cris LaPierre
Cris LaPierre 2020년 3월 22일
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 2020년 3월 22일
편집: Keegan Carvalho 2020년 3월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 NetCDF에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by