MATLAB Answers

How to treat a duration array in a for loop?

4 views (last 30 days)
JMSE on 3 Aug 2021
Edited: Stephen on 3 Aug 2021
My overall goal: I have measurements from different sensors which are performed simoultaneously, but with different timesteps. Now, I want to use both outputs in order to do some calculations.
So, I need to calculate mean values from some measurements (e.g. timesteps of each 10sec) in order to put them into the same format likewise the other measurements (timesteps of each 10min). In the end I want to have the different measurements with equal timesteps.
Hence, one table and its timesteps represent a pattern, and I want to convert the other table into the same pattern with regard to the timesteps.
Therefore, I have started to build up a for loop to write a table with (1) the timesteps and (2) the corresponding calculated mean values. I have started with the timesteps, but I am facing an issue with the duration array within this for loop. It gives me "Error using duration/double".
My short code so far:
%% read input file (csv) and determine output path
in_timesteps = 'C:\Users\Judith\Desktop\phd\Data\Hukseflux\Calculations\26.07.2021_28.07.2021_calc_Enet.csv';
in_measured = 'C:\Users\Judith\Desktop\phd\Data\daten_wetterstation\WL__2021_07_27.csv';
measure_day = '27.07.2021';
output = 'C:\Users\Judith\Desktop\phd\Data\Hukseflux\Calculations\';
data_timesteps = readtable(in_timesteps, 'NumHeaderLines', 0, 'Delimiter', ',' ,'DecimalSeparator', '.', "VariableNamingRule" , "preserve");
data_measured = readtable(in_measured, 'NumHeaderLines', 0, 'Delimiter', ',' ,'DecimalSeparator', '.', "VariableNamingRule" , "preserve");
data_measured.Properties.VariableNames = regexprep(data_measured.Properties.VariableNames, ' ', '_');
%date = unique(data_timesteps.Date);
ind_dt = find(data_timesteps.Date == measure_day);
H = zeros(sum(data_timesteps.Date == measure_day),2);
for i = 1:(sum(data_timesteps.Date == measure_day))
H(i,1) = data_timesteps.Time(ind_dt(i))
% ind_time = find(data_measured.Time > data_timesteps.Time(1) & data_measured.Time < data_timesteps.Time(2) & data_measured.Date == measure_day);
% x = mean(data_measured.WD__Avr(ind_time));
% new_table = table(data_timesteps.Time(2), x);
Any help is very much appreciated.
  1 Comment
JMSE on 3 Aug 2021
I have found an easier way to do this:
t = table(data_timesteps.Time(ind_dt),zeros(sum(data_timesteps.Date == measure_day)));
Thank you, anyways.

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 3 Aug 2021
Judith, I can't really follow what you are doing, but if you have timestamped data, you should be using a timetable, and then retime makes aggregation (what it looks like you are doing) a one-liner, perhaps something like:
tt2 = retime(tt,'daily','sum')
If you have two timetables, synchronizes makes matching them up by time a one liner, perhaps something like
tt12 = synchronize(tt1,tt2,'first','linear')

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by