Code covered by the BSD License

# Working with Time-Series Data in MATLAB

by

### Abhaya (view profile)

29 Jan 2013 (Updated )

These are the example files for the webinar: "Working with Time-Series Data in MATLAB".

format_data.m
```%%
% Copyright 2012-2013 The MathWorks, Inc.

% break out the data into 3 subsets where the sampling times and offsets
% are different

%% set 1

time1 = v_B1.time;

FR1 = v_B1.signals(1).values.*(1 + 0.002*randn(numel(time1),1))*5/6;
VM1 = v_B1.signals(2).values.*(1 + 0.002*randn(numel(time1),1));
VA1 = v_B1.signals(1).values*0;

tt1 = '19-JAN-2012 14:00:00.000'; % AEDT
tt2 = '19-JAN-2012 13:30:00.000'; % ACDT
tt3 = '19-JAN-2012 13:00:00.000'; % AEST

ss1 = datenum(tt1);
ss2 = datenum(tt2);
ss3 = datenum(tt3);

for tl = 1:numel(time1);

tc1{tl,1} = [datestr(ss1+time1(tl)/3600/24,'yyyy-mm-dd HH:MM:SS.FFF'),'  AEDT']; %#ok<*SAGROW>

end

time2a = (0:1/120:150)';

time2 = time2a + 3/1000;

t2 = sort([time2a;time2]);

tc2 = cell(numel(time2),1);

for tl = 1:numel(time2)-1;

tc2{tl,1} = [datestr(ss2+time2(tl)/3600/24,'yyyy-mm-dd HH:MM:SS.FFF'),'  ACDT'];

end

time3 = time1(1:2:end)+45/1000;

t3 = sort([time1(1:2:end);time3]);

tc3 = cell(numel(time3),1);

for tl = 1:numel(time3)-1;

tc3{tl,1} = [datestr(ss3+time3(tl)/3600/24,'yyyy-mm-dd HH:MM:SS.FFF'),'  AEST'];

end

for l = 2:15

nl = num2str(l);

eval(['FR',nl,' = v_B',nl,'.signals(1).values.*(1 + 0.001*randn(numel(time1),1))*5/6;']); % *5/6 to bring to 50Hz for Australia
eval(['VM',nl,' = v_B',nl,'.signals(2).values.*(1 + 0.001*randn(numel(time1),1));']);
eval(['VA',nl,' = v_B',nl,'.signals(3).values - v_B1.signals(3).values;']);
eval(['VA',nl,' = VA',nl,'.*(1 + 0.001*randn(numel(time1),1));']);

if l > 5 && l <= 10 % resample to 120Hz and then offset by 3/1000s

eval(['FRi = interp1(time1,FR',nl,',t2);']);
eval(['VMi = interp1(time1,VM',nl,',t2);']);
eval(['VAi = interp1(time1,VA',nl,',t2);']);

eval(['FR',nl,' = FRi(2:2:end);'])
eval(['VM',nl,' = VMi(2:2:end);'])
eval(['VA',nl,' = VAi(2:2:end);'])

end

if l > 10 % resample to 30 Hz amd then offset by 45/1000s

eval(['FR',nl,' = FR',nl,'(1:2:end);']);
eval(['VM',nl,' = VM',nl,'(1:2:end);']);
eval(['VA',nl,' = VA',nl,'(1:2:end);']);

eval(['FRi = interp1(time1(1:2:end),FR',nl,',t3);'])
eval(['VMi = interp1(time1(1:2:end),VM',nl,',t3);'])
eval(['VAi = interp1(time1(1:2:end),VA',nl,',t3);'])

eval(['FR',nl,' = FRi(2:2:end);'])
eval(['VM',nl,' = VMi(2:2:end);'])
eval(['VA',nl,' = VAi(2:2:end);'])

end

end

%%

%VM2(124) = NaN;

names1 = {'Time';'VM1';'VM2';'VM3';'VM4';'VM5';'VA1';'VA2';'VA3';'VA4';'VA5';'FR1';'FR2';'FR3';'FR4';'FR5'};

data1 = dataset(tc1,VM1,VM2,VM3,VM4,VM5,VA1,VA2,VA3,VA4,VA5,FR1,FR2,FR3,FR4,FR5,'VarNames',names1);

%%

names2 = {'Time';'VM6';'VM7';'VM8';'VM9';'VM10';'VA6';'VA7';'VA8';'VA9';'VA10';'FR6';'FR7';'FR8';'FR9';'FR10'};

data2 = dataset(tc2,VM6,VM7,VM8,VM9,VM10,VA6,VA7,VA8,VA9,VA10,FR6,FR7,FR8,FR9,FR10,'VarNames',names2);

%%

names3 = {'Time';'VM11';'VM12';'VM13';'VM14';'VM15';'VA11';'VA12';'VA13';'VA14';'VA15';'FR11';'FR12';'FR13';'FR14';'FR15'};

data3 = dataset(tc3,VM11,VM12,VM13,VM14,VM15,VA11,VA12,VA13,VA14,VA15,FR11,FR12,FR13,FR14,FR15,'VarNames',names3);

%% write to excel files

xlswrite('data1.xlsx',dataset2cell(data1))
xlswrite('data2.xlsx',dataset2cell(data2))
xlswrite('data3.xlsx',dataset2cell(data3))
```