Reading netcdf files and variables backwards

2 views (last 30 days)
I have a number of files, each file has numerous variables that I need to read backwards (each file corresponds to one day, I need to read all the info in each file backwards and I also need to start reading from file number 15 to file number 1). So far, I've been able to start reading from file 15 to file 1. But when reading the variables also backwards I'm still stuck ( In my code, I'm selecting just 1 variable to verify it's working and I get all the data backwards). Still, I don't know what I'm doing wrong in here.
clear myFolder = ('C:\modelana\netcdf_2019'); fileList = dir([myFolder '*.nc']);
for k = length(fileList):-1:1 %read filelist backwards if isempty(fileList) continue; else baseFileName = fileList(k).name; fullFileName = fullfile(myFolder, baseFileName);
ncfile=[myFolder fileList(k).name];
s{k} = ncread(ncfile,'salinity');
for s = length(k):-1:1 %read contents of array k backwards
if isempty(k)
continue;
else
baseFileName = k(s).name;
fullFileName = fullfile(myFolder, baseFileName);
ncfile = [myFolder k(s).name];
end
end
end
%t{k} = ncread(ncfile,'temp');
%u{k} = ncread(ncfile,'u');
end '''
  2 Comments
KSSV
KSSV on 11 Sep 2020
Why you are worried about reading in backwards?
Ana Corrochano
Ana Corrochano on 15 Sep 2020
I have to do it for my project. At some point I have to simulate a particle tracking movement, and I need the particles starting moving in the end point

Sign in to comment.

Answers (1)

Madhav Thakker
Madhav Thakker on 15 Sep 2020
Hi Ana,
I understand that you want to read the variables backward. I see that there is some problem with naming of the variables.
for k = length(fileList):-1:1 %read filelist backwards if isempty(fileList) continue; else baseFileName = fileList(k).name; fullFileName = fullfile(myFolder, baseFileName);
k is an integer here, which is causing problems in,
for s = length(k):-1:1 %read contents of array k backwards
if isempty(k)
continue;
else
baseFileName = k(s).name;
fullFileName = fullfile(myFolder, baseFileName);
ncfile = [myFolder k(s).name];
end
end
In this loop, there are multiple references to variable k. This should be s{k} instead.
Hope this helps.

Community Treasure Hunt

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

Start Hunting!