Line plots from netcdf files

5 views (last 30 days)
Rory Bateson
Rory Bateson on 6 May 2021
Answered: Asvin Kumar on 12 May 2021
Hi, I'm currently in the process of trying to extract data from a netcdf file which contain 'sea surface temperature' for the Atlantic ocean. I have defined the variable I require (sst), I used the for function to create a mean of the years I want however i'm not sure if this is done correctly.
for n=1:21
for m=1:12
sst=mean(1:21); % the data is from 2000-2020
sst=mean(1:12); % months
end
end
the next problem is I do not know where to begin with my line plot. Any help would be appreciated.

Answers (1)

Asvin Kumar
Asvin Kumar on 12 May 2021
I'm not entirely clear as to what you want to plot. Since you seem like you're new to the language, let me give you a brief outline of the steps involved for you to get any plots of your choice.
1. Load data from netcdf files
Use ncread to read the required data from NetCDF files. The linked doc page has many examples. Here's a more detailed example: Read from NetCDF Files using High-Level Functions.
ncread is a high-level MATLAB function. MATLAB also provides access to low-level functions from the NetCDF Library Package. You can read more about all available function for handling NetCDF files over here.
2. Processing your data
I am going to assume the variable 'sst' has the data from the NetCDF files. I'm also going to assume that the size of 'sst' is 21x12 where each row corresponds to an year from 2000 to 2020 and each columns corresponds to a month.
I am not clear about why/what you want to take the average over since I am not familiar with the structure of your data. Here's an example on using the mean function. You don't need to use for loops, MATLAB can compute the mean across the dimension that you specify.
dummyData = ones(21,12);
dummyDataAvgPerMonth = mean(dummyData, 1); % mean across dimension 1
whos dummyDataAvgPerMonth % shows details about the variable
Name Size Bytes Class Attributes dummyDataAvgPerMonth 1x12 96 double
dummyDataAvgPerYear = mean(dummyData, 2); % mean across dimension 2
whos dummyDataAvgPerYear % shows details about the variable
Name Size Bytes Class Attributes dummyDataAvgPerYear 21x1 168 double
If my assumptions about the data are wrong and you have hourly or daily data that you want to average over, you can use similar approaches to the example and average over the required dimension.
3. Plot
Once you have the processed data, it's time to plot.
If you want to plot the trends of the sea surface temperature for every month over 21 years, you can create a figure with 12 lines (1 for each month) using the plot function. Refer this example to create a line plot from a matrix.
If you want to plot the trends of the sea surface temperature for each year from Jan to Dec, you could use the same approach as above but instead use the transposed version of the matrix.
If instead, you have daily data and you want to create line plots wrt to time, refer this example: Plot Dates and Durations.
Here's an example of how to add a legend to your plot.
---
Hope this helps you get started. If you need more help, please provide more details about the data you are working with. Also provide details about the charts that you are trying to plot. If you're following a reference, please attach it as that might be helpful.
Since, you're new to the language, I would strongly recommend trying out MATLAB Onramp. It will hardly take 1-2 hours. You will have the basics set once you're done with the course. You can work on the sections that will be immediately useful to you.

Products

Community Treasure Hunt

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

Start Hunting!