Reading netcdf files and variables backwards

조회 수: 2 (최근 30일)
Ana Corrochano
Ana Corrochano 2020년 9월 11일
답변: Madhav Thakker 2020년 9월 15일
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
KSSV
KSSV 2020년 9월 11일
Why you are worried about reading in backwards?
Ana Corrochano
Ana Corrochano 2020년 9월 15일
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

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

답변 (1개)

Madhav Thakker
Madhav Thakker 2020년 9월 15일
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.

카테고리

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