Reference to non-existent field 'folder'.

2 ビュー (過去 30 日間)
MAT NIZAM UTI
MAT NIZAM UTI 2024 年 3 月 11 日
回答済み: Walter Roberson 2024 年 3 月 11 日
Hi I have this coding to extract data from multiple .nc file, however, during running the code there is an error
Here is the code:
clear all;
clc;
files = dir('*.nc');
outputDirectory = '2023_try'; %create your .asc folder eg: 202201
% Create the output directory if it doesn't exist
if ~exist(outputDirectory, 'dir')
mkdir(outputDirectory);
end
for k = 1:length(files)
ncfile = fullfile(files(k).folder, files(k).name);
%SST = ncread(ncfile, 'SST');
%SSSanomaly = ncread(ncfile, 'SSS_anom');
%SSSuncorrected = ncread(ncfile, 'SSS_uncorr');
SSS = ncread(ncfile, 'SSS_corr');
Lat = ncread(ncfile, 'Latitude');
Lon = ncread(ncfile, 'Longitude');
Data = [Lat(:), Lon(:), SSS(:)] %SSSuncorrected(:), SSSanomaly(:), SST(:)];
id = Lat>=0 & Lat<=14 & Lon>=95 & Lon<=126;
Data1 = Data(id,:,:);
% Data1(isnan(Data1(:,3)),:) = [];
%Data1(isnan(Data1(:,4)),:) = [];
%Data1(isnan(Data1(:,5)),:) = [];
%Data1(isnan(Data1(:,6)),:) = [];
%id = (Lat >= 0 & Lat <= 14);
%Data1 = Data(id, :);
% Extract the filename without the extension
[~, baseFileName, ~] = fileparts(ncfile);
% Create the output .asc filename
outputFileName = fullfile(outputDirectory, [baseFileName '.asc']);
% Save the data as an ASCII file
save(outputFileName, 'Data1', '-ASCII');
end
Here is the error:
Reference to non-existent field 'folder'.
Error in SMOS_nc_extract_all (line 10)
ncfile = fullfile(files(k).folder, files(k).name);

回答 (2 件)

Jonas
Jonas 2024 年 3 月 11 日
編集済み: Jonas 2024 年 3 月 11 日
if there are no .nc files in the current directory, then
files = dir('*.nc');
will be empty, and as consequence
files(k)
will be referring to an empty struct and the field
files(k).folder
will not exist
  1 件のコメント
MAT NIZAM UTI
MAT NIZAM UTI 2024 年 3 月 11 日
In the current directory, there is .nc file.. but there is still same error..

サインインしてコメントする。


Walter Roberson
Walter Roberson 2024 年 3 月 11 日
In sufficiently old versions of MATLAB, there is no .folder field in the results of dir.
I think .folder was added roughly R2013b

カテゴリ

Help Center および File ExchangeNetCDF についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by