Converting .nc files to an array and save it in a .txt

I've been working on a code to read the information of the GRACE mission the files are .nc and my Professor asked me to create an array where I save the statistics of the entire folder where all the files are, I need to save the averages of the results to a .txt file.
Can somebody guide me to some video or give me some sort of direction on what to do?
Thank you.

Answers (1)

6 Comments

The problem that I´m having is the outputs that I want to save are statistics and figure but i dont know how to program it to run the folder which contains 163 NetCDF files and save all the statistics and figure of every single one of those files on that folder.
Can we assume you have written code that does everything you need for a single file?
If so, then it is just a matter of running that code for every file. There are several ways to do that. You'll find a myriad of answers here about how to load multiple files/loop through all files in a folder/etc.
Here's one example that uses dir and a for loop.
clear all;
close all;
clc
format long g
filename = 'C:\Users\vasco\OneDrive\Desktop\work\JPL TELLUS GRACE Level-3\GRD-3_2017100-2017128_GRAC_JPLEM_BA01_0600_LND_v04.nc';
nc=netcdf.open(filename);
ncdisp(filename);
time = double(ncread(filename, 'time'));
tm = string(time);
file_fig = strcat('GRACE_',tm,'.png');
lat=double(ncread(filename, 'lat'));
lon=double(ncread(filename, 'lon'));
lwe = ncread(filename, 'lwe_thickness');
[x,y] = meshgrid(lon,lat);
figure(1)
axis equal
clev=-2.0:0.1:0.5;
hc = contourfcmap(x,y,lwe',clev,winter(length(clev)-1), ...
'lo', [.8 .8 .8], ...
'hi', [.2 .2 .2], ...
'cbarloc', 'eastoutside', ...
'method', 'calccontour',...
'evencb', true);
title(strcat('GRACE-',tm));
I have to run this for every file on the folder
Sounds like a for loop, then. That is the solution show in the Answers post I linked to above. Can you make an attempt at taking the solution shared there and adapting it to work with your code?
of course I can thank you and sorry for bothering

Sign in to comment.

Asked:

on 24 Jul 2023

Edited:

on 24 Jul 2023

Community Treasure Hunt

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

Start Hunting!