Saving data from a for loop (new-ish to MATLAB!)

I have the following for loop to get barometric pressure data from the NOAA tide buoy in Bar Harbor, ME (NOAA only provides data in one month increments and I need data for three months, hence the loop)
mn_start=9; %month to start
dd_start=11; %day to start
date_BH=[];baro_BH=[];%initializing datenum and baro variables
num_mnths=3; %number of consective months to loop - note coded to just to yr=2022
for i=1:num_mnths;
begin_date=['2022' num2str(mn_start+i-1,'%02.f') num2str(dd_start)];
end_date=['2022' num2str(mn_start+i,'%02.f') num2str(dd_start-1)];
NOAAurl_Baro=['https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?' ...
'begin_date=', begin_date, '&end_date=' end_date , '&station=8413320&'...
'product=air_pressure&datum=MSL&time_zone=lst_ldt&units=metric&format=csv'];
datatable=webread(NOAAurl_Baro);%getting waterlevel data from NOAA
date_BH=[date_BH table2array(datatable(:,1))'];%serial date number
baro_BH=[baro_BH table2array(datatable(:,2))'];%barometric pressure (mbars); datenum same as WL
end
When the loop closes, I only have data saved from the final iteration. How can I save data from every iteration? Thank you!

 采纳的回答

Save the ‘datatable’ output to a cell array —
mn_start=9; %month to start
dd_start=11; %day to start
date_BH=[];baro_BH=[];%initializing datenum and baro variables
num_mnths=3; %number of consective months to loop - note coded to just to yr=2022
for i=1:num_mnths;
begin_date=['2022' num2str(mn_start+i-1,'%02.f') num2str(dd_start)];
end_date=['2022' num2str(mn_start+i,'%02.f') num2str(dd_start-1)];
NOAAurl_Baro=['https://api.tidesandcurrents.noaa.gov/api/prod/datagetter?' ...
'begin_date=', begin_date, '&end_date=' end_date , '&station=8413320&'...
'product=air_pressure&datum=MSL&time_zone=lst_ldt&units=metric&format=csv'];
datatable{i}=webread(NOAAurl_Baro);%getting waterlevel data from NOAA
% date_BH=[date_BH table2array(datatable(:,1))'];%serial date number
% baro_BH=[baro_BH table2array(datatable(:,2))'];%barometric pressure (mbars); datenum same as WL
end
datatable{1}
ans = 7200×5 table
DateTime Pressure X N R ________________ ________ _ _ _ 2022-09-11 00:00 1020.3 0 0 0 2022-09-11 00:06 1020.3 0 0 0 2022-09-11 00:12 1020.3 0 0 0 2022-09-11 00:18 1020.3 0 0 0 2022-09-11 00:24 1020.3 0 0 0 2022-09-11 00:30 1020.3 0 0 0 2022-09-11 00:36 1020.3 0 0 0 2022-09-11 00:42 1020.2 0 0 0 2022-09-11 00:48 1020.2 0 0 0 2022-09-11 00:54 1020.3 0 0 0 2022-09-11 01:00 1020.2 0 0 0 2022-09-11 01:06 1020.2 0 0 0 2022-09-11 01:12 1020.3 0 0 0 2022-09-11 01:18 1020.3 0 0 0 2022-09-11 01:24 1020.3 0 0 0 2022-09-11 01:30 1020.3 0 0 0
datatable{2}
ans = 7440×5 table
DateTime Pressure X N R ________________ ________ _ _ _ 2022-10-11 00:00 1023.2 0 0 0 2022-10-11 00:06 1023.2 0 0 0 2022-10-11 00:12 1023.2 0 0 0 2022-10-11 00:18 1023.2 0 0 0 2022-10-11 00:24 1023.2 0 0 0 2022-10-11 00:30 1023.2 0 0 0 2022-10-11 00:36 1023.3 0 0 0 2022-10-11 00:42 1023.2 0 0 0 2022-10-11 00:48 1023.2 0 0 0 2022-10-11 00:54 1023.2 0 0 0 2022-10-11 01:00 1023.1 0 0 0 2022-10-11 01:06 1023.2 0 0 0 2022-10-11 01:12 1023.3 0 0 0 2022-10-11 01:18 1023.3 0 0 0 2022-10-11 01:24 1023.3 0 0 0 2022-10-11 01:30 1023.3 0 0 0
datatable{3}
ans = 7200×5 table
DateTime Pressure X N R ________________ ________ _ _ _ 2022-11-11 00:00 1015 0 0 0 2022-11-11 00:06 1015.1 0 0 0 2022-11-11 00:12 1015.2 0 0 0 2022-11-11 00:18 1015.2 0 0 0 2022-11-11 00:24 1015.2 0 0 0 2022-11-11 00:30 1015.3 0 0 0 2022-11-11 00:36 1015.4 0 0 0 2022-11-11 00:42 1015.4 0 0 0 2022-11-11 00:48 1015.3 0 0 0 2022-11-11 00:54 1015.2 0 0 0 2022-11-11 01:00 1015.3 0 0 0 2022-11-11 01:06 1015.3 0 0 0 2022-11-11 01:12 1015.4 0 0 0 2022-11-11 01:18 1015.5 0 0 0 2022-11-11 01:24 1015.7 0 0 0 2022-11-11 01:30 1015.8 0 0 0
cat_datable = cat(1, datatable{:}) % Cioncatenate All To A Single Table (Optional)
cat_datable = 21840×5 table
DateTime Pressure X N R ________________ ________ _ _ _ 2022-09-11 00:00 1020.3 0 0 0 2022-09-11 00:06 1020.3 0 0 0 2022-09-11 00:12 1020.3 0 0 0 2022-09-11 00:18 1020.3 0 0 0 2022-09-11 00:24 1020.3 0 0 0 2022-09-11 00:30 1020.3 0 0 0 2022-09-11 00:36 1020.3 0 0 0 2022-09-11 00:42 1020.2 0 0 0 2022-09-11 00:48 1020.2 0 0 0 2022-09-11 00:54 1020.3 0 0 0 2022-09-11 01:00 1020.2 0 0 0 2022-09-11 01:06 1020.2 0 0 0 2022-09-11 01:12 1020.3 0 0 0 2022-09-11 01:18 1020.3 0 0 0 2022-09-11 01:24 1020.3 0 0 0 2022-09-11 01:30 1020.3 0 0 0
.

2 个评论

Thank you for your help! This is exactly what I was looking for.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by