MATLAB Answers

Read every file in a folder

3 views (last 30 days)
Valerio Gianforte
Valerio Gianforte on 24 Feb 2020
Commented: Stephen Cobeldick on 25 Feb 2020
Hi everyone,
I should read and make more operation on the files inside in a folder. What's the faster method? Thanks.
file = 'ww3_outf_198501.nc';
ncdisp(file);
lon = ncread(file,'x');
lat = ncread(file,'y');
H_s = ncread(file,'Hs');
T_m = ncread(file,'Tm');
D_m = ncread(file,'Dm');
time1 = ncread(file,'time1');
time2 = ncread(file,'time2');
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
Hs1(1,:) = H_s(PLon,PLat,:);
Hs2(1,:) = H_s(PLon_1,PLat_1,:);
Hs3(1,:) = H_s(PLon_1,PLat,:);
Hs4(1,:) = H_s(PLon,PLat_1,:);
Hs1 = (Hs1(1,:)).';
Hs2 = (Hs2(1,:)).';
Hs3 = (Hs3(1,:)).';
Hs4 = (Hs4(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);
prod_sum = (Hs2 * d2) + (Hs3 * d3);
sum_d = d2 + d3;
Hs_buoy = prod_sum / sum_d;
Tm1(1,:) = T_m(PLon,PLat,:);
Tm2(1,:) = T_m(PLon_1,PLat_1,:);
Tm3(1,:) = T_m(PLon_1,PLat,:);
Tm4(1,:) = T_m(PLon,PLat_1,:);
Tm1 = (Tm1(1,:)).';
Tm2 = (Tm2(1,:)).';
Tm3 = (Tm3(1,:)).';
Tm4 = (Tm4(1,:)).';
prod_sum = (Tm2 * d2) + (Tm3 * d3);
sum_d = d2 + d3;
Tm_buoy = prod_sum / sum_d;
Dm1(1,:) = D_m(PLon,PLat,:);
Dm2(1,:) = D_m(PLon_1,PLat_1,:);
Dm3(1,:) = D_m(PLon_1,PLat,:);
Dm4(1,:) = D_m(PLon,PLat_1,:);
Dm1 = (Dm1(1,:)).';
Dm2 = (Dm2(1,:)).';
Dm3 = (Dm3(1,:)).';
Dm4 = (Dm4(1,:)).';
prod_sum = (Dm2 * d2) + (Dm3 * d3);
sum_d = d2 + d3;
Dm_buoy = prod_sum / sum_d;
time_1 = single(time1);
time_2 = single(time2);
Data_matrix = [time_1 time_2 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_matrix');

  0 Comments

Sign in to comment.

Answers (1)

Bob Nbob
Bob Nbob on 24 Feb 2020
To get information on all the files in a folder use dir, or getdir. From there, just loop through the elements of the dir output and conduct your desired operation on each.

  2 Comments

Valerio Gianforte
Valerio Gianforte on 25 Feb 2020
Sorry, this is the first time that I use Matlab, can you text me the code that I should to write? Thanks

Sign in to comment.

Products


Release

R2019b