How to save data from every file in a folder
Afficher commentaires plus anciens
Hi everyone,
I made the code below that read and compute every file in a folder with for loop. Now, I should to save the data (time1, time2, Hs_buoy, Tm_buoy, Dm_buoy) of each file in a table but I have no idea how to do. For now, the code save the data of first file only. Thanks.
folderData = 'D:\Valerio\data\TEST';
filePattern = fullfile(folderData, '*.nc');
ncfiles = dir(filePattern);
nFiles = length(ncfiles);
for i = 1:nFiles
filename = fullfile(ncfiles(i).folder, ncfiles(i).name);
ncdisp(filename);
lon = ncread(filename,'x');
lat = ncread(filename,'y');
%latitudine e longitudine
H_s = ncread(filename,'Hs');
T_m = ncread(filename,'Tm');
D_m = ncread(filename,'Dm');
time1 = ncread(filename,'time1'); %anno, mese e giorno
time2 = ncread(filename,'time2'); %ore, minuti e secondi
%uso lat=39.51 e lon=-0.3 che rappresenta la boa a largo di Valencia
buoy_lat = 39.51;
buoy_lon = -0.30;
S_lat = abs(buoy_lat - lat);
S_lon = abs(buoy_lon - lon);
F_lat = find(S_lat == min(S_lat));
F_lon = find(S_lon == min(S_lon));
Lat_1 = lat(F_lat);
Lon_1 = lon(F_lon);
if (buoy_lat > Lat_1);
PLat = find(lat == Lat_1);
PLat_1 = PLat + 1;
Lat_2 = lat(PLat_1);
else
PLat = find(lat == Lat_1);
PLat_1 = PLat - 1;
Lat_2 = lat(PLat_1);
end
if (buoy_lon > Lon_1)
PLon = find(lon == Lon_1)
PLon_1 = PLon + 1
Lon_2 = lon(PLon_1)
else
PLon = find(lon == Lon_1)
PLon_1 = PLon - 1
Lon_2 = lon(PLon_1)
end
%calcolo Hs
Hs_1 = H_s(PLon,PLat,:);
Hs_2 = H_s(PLon_1,PLat_1,:);
Hs_3 = H_s(PLon_1,PLat,:);
Hs_4 = H_s(PLon,PLat_1,:);
Hs1 = (Hs_1(1,:)).';
Hs2 = (Hs_2(1,:)).';
Hs3 = (Hs_3(1,:)).';
Hs4 = (Hs_4(1,:)).';
DLon = Lon_2 - abs(buoy_lon);
DLat_2 = buoy_lat - Lat_2;
DLat_3 = Lat_1 - buoy_lat;
d2 = sqrt(DLon^2 + DLat_2^2);
d3 = sqrt(DLon^2 + DLat_3^2);
%interpolazione lineare per ricavare i dati sulla boa
prod_sum = (Hs2 * d2) + (Hs3 * d3);
sum_d = d2 + d3;
Hs_buoy = prod_sum / sum_d;
%calcolo Tm
Tm_1 = T_m(PLon,PLat,:);
Tm_2 = T_m(PLon_1,PLat_1,:);
Tm_3 = T_m(PLon_1,PLat,:);
Tm_4 = T_m(PLon,PLat_1,:);
Tm1 = (Tm_1(1,:)).';
Tm2 = (Tm_2(1,:)).';
Tm3 = (Tm_3(1,:)).';
Tm4 = (Tm_4(1,:)).';
prod_sum = (Tm2 * d2) + (Tm3 * d3);
sum_d = d2 + d3;
Tm_buoy = prod_sum / sum_d;
%calcolo Dm
Dm_1 = D_m(PLon,PLat,:);
Dm_2 = D_m(PLon_1,PLat_1,:);
Dm_3 = D_m(PLon_1,PLat,:);
Dm_4 = D_m(PLon,PLat_1,:);
Dm1 = (Dm_1(1,:)).';
Dm2 = (Dm_2(1,:)).';
Dm3 = (Dm_3(1,:)).';
Dm4 = (Dm_4(1,:)).';
prod_sum = (Dm2 * d2) + (Dm3 * d3);
sum_d = d2 + d3;
Dm_buoy = prod_sum / sum_d;
time1_str = num2str(time1);
time1_date = datetime(time1_str,'InputFormat','yyyyMMdd','Format','yyyy/MM/dd');
Data_table = table(time1_date, time2, Hs_buoy, Tm_buoy, Dm_buoy);
%x = time1;
%y = Hs_buoy;
%plot(x,y,'--ro');
%xlabel('time1');
%ylabel('Hs');
%title('trend of significant wave height');
%nameFile = 'Data.mat';
%save(nameFile, 'Data_table');
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!