How to save data from every file in a folder

조회 수: 1 (최근 30일)
Valerio Gianforte
Valerio Gianforte 2020년 2월 27일
댓글: Valerio Gianforte 2020년 2월 27일
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

채택된 답변

Bhaskar R
Bhaskar R 2020년 2월 27일
It is not recommended to create table in loop, get the data in variable(say cell data) then form table out of the loop
folderData = 'D:\Valerio\data\TEST';
filePattern = fullfile(folderData, '*.nc');
ncfiles = dir(filePattern);
nFiles = length(ncfiles);
% initialize variables
time1 = cell(nFiles, 1);
time2 = cell(nFiles, 1);
Hs_buoy = cell(nFiles, 1);
Tm_buoy = cell(nFiles, 1);
Dm_buoy = cell(nFiles, 1);
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{i} = ncread(filename,'time1'); %anno, mese e giorno
time2{i} = 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{i} = 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{i} = 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{i} = prod_sum / sum_d;
time1_str = num2str(time1);
time1_date{i} = 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
Data_table = table(time1_date, time2, Hs_buoy, Tm_buoy, Dm_buoy);
  댓글 수: 7
Bhaskar R
Bhaskar R 2020년 2월 27일
That means your all variables must have the same length, check each variable those are inputs to the table after loop
Valerio Gianforte
Valerio Gianforte 2020년 2월 27일
I fixed the problem but I would like to obtain a table 125x5 that contain all data from the files. Now, I have a table 12x5 where the each element is a vector with dimension 125x5. Is there a way to do this?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Display and Presentation에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by