Splitting data into sets of 20 to store in a variable

1 view (last 30 days)
Hello I am trying to split the data from Open, High, Low and Close into sets of every 20 days. I want DataE20 (data every 20 days) to look something like this:
day1 day2 ..... day21
DataE20(1) = Open(1) Open(2) ..... Open(21)
High(1) High(2) ..... High(21)
Low(1) Low(2) ..... Low(21)
Close(2) Close(2) ..... Close(20)
day22 day23 ..... day42
DataE20(2) = Open(22) Open(23) ..... Open(42)
High(22) High(23) ..... High(42)
Low(22) Low(23) ..... Low(42)
Close(22) Close(23) ..... Close(42)
and so on until DataE20 cant go any longer.
data =readtable('EURUSD=X.csv')
Open = data(:,2);
High= data(:,3);
Low = data(:,4);
Close = data(:,5);
stackedplot(Open)
Data20(1:height(Open)/20) =
I have attached what the table looks like. Any help would be greatly appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Nov 2022
Dates = data.Date;
dayoffset = days(Dates - Dates(1));
G = floor(dayoffset/20) + 1;
DataE20 = splitapply(@(Date,Open,High,Low,Close,AdjClose,Volume) {[Open,High,Low,Close].'}, Data, G);
The difference between this and what you layed out is that this does not assume that data is present for all dates. It groups by 20 consecutive dates, not by 20 consecutive file entries. The file appears to not have entries for weekends. My code will not split by 20 entries (4 calendar weeks if the only missing entries correspond to weekends), it splits by 20 days (slightly less than 3 calendar weeks), which is what you asked for.
  5 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!