how to create a STRUCTURE composed of variables from .NC files

I have a very heavy set of .NC files (ERA 5 from Copernicus) to use within other scripts. They are the data relating to the characteristics of the winds.
I would therefore like to divide the .NC files into a STRUCTURE composed of all the variables present within the files, so that I can insert that structure within the other scripts and not re-read the.NC files from scratch, but taking into consideration directly the variable that interests me within the structure.
the variables are lat, long, altitude, time, z, u, v.
Can anyone help me?

2 Comments

You can read the file then and there...why you want to fill the memory by reading the whole file and conevrting it into a variable?
because it was suggested to me to make other scripts run faster

Sign in to comment.

Answers (1)

source = 'something appropriate';
vars = ["lat", "long", "altitude", "time", "z", "u", "v"];
for var = vars
HeavyStruct.(var) = ncread(source, var);
end

6 Comments

i wrote:
source = '1990-1993.nc';
vars = ['lat90','long90','level90','time90','Z90','U90','V90'];
for var = vars
HeavyStruct.(var) = ncread('1990-1993.nc', var);
end
but it doesn't work and it gives me this error:
Error using internal.matlab.imagesci.nc/getGroupAndVarid (line 2132)
Could not find variable or group 'l' in file.
Error in internal.matlab.imagesci.nc/read (line 632)
[gid, varid] = getGroupAndVarid(this, location);
Error in ncread (line 66)
vardata = ncObj.read(varName, varargin{:});
what can I do?
You can use the format for vars that I showed, unless you are using R2016b or earlier.
source = '1990-1993.nc';
vars = ["lat90", "long90", "level90", "time90", "Z90", "U90", "V90"];
for var = vars
HeavyStruct.(var) = ncread('1990-1993.nc', var);
end
If you are using R2016b or earlier then
source = '1990-1993.nc';
vars = {'lat90','long90','level90','time90','Z90','U90','V90'};
for varidx = 1 : length(vars);
var = vars{varidx};
HeavyStruct.(var) = ncread('1990-1993.nc', var);
end
it doesn't work.. my matlab version is R2021b
What error message are you seeing?
i wrote:
source = '1990-1993.nc';
vars = {'lat90','long90','level90','time90','Z90','U90','V90'};
for varidx = 1 : length(vars)
var = vars{varidx};
HeavyStruct.(var) = ncread('1990-1993.nc', var);
end
and the errors are:
Error using internal.matlab.imagesci.nc/getGroupAndVarid (line 2132)
Could not find variable or group 'lat90' in file.
Error in internal.matlab.imagesci.nc/read (line 632)
[gid, varid] = getGroupAndVarid(this, location);
Error in ncread (line 66)
vardata = ncObj.read(varName, varargin{:});
Error in struttura_nc_script (line 19)
HeavyStruct.(var) = ncread('1990-1993.nc', var);
Earlier, when you gave the names of the variables, none of the names ended in "90", but you then changed so that they all end in "90". What does ncinfo say that the variable names are?

Sign in to comment.

Asked:

on 6 Jul 2022

Commented:

on 10 Jul 2022

Community Treasure Hunt

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

Start Hunting!