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!

 Respuesta aceptada

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 comentarios

Molly Autery
Molly Autery el 6 de En. de 2023
Thank you for your help! This is exactly what I was looking for.
Star Strider
Star Strider el 6 de En. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Financial Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 6 de En. de 2023

Comentada:

el 6 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by