How to delete a rows in a timetable for the rows (dates) that are not trading days?
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi all,
I currently have a timetable with 3 columns: dates, assets, signals ("dates" column is not numbered so I am assuming that the timetable perceives it as having 2 non-date/time columns).
My data is in a form of timetable, but it looks likes this (I tried).
| 1 | 2
dates | assets | val
------------------------------
08-Apr-2013| 'fixed' | 2
.
.
.
.
Not sure if my rows can be numbered with integers starting from 1, or if my rows are already numberd with dates.
For my "dates" column, I have every single day from a start date to a certain start date, but I want to write a code that deletes the rows that are not business trading days.
One way that I thought of is:
- Create a vector of dates that are holiday
H = holidays(startdate, enddate)
2. Check for each row if the row's date is a member of nontrading days, H
for row:n
if ismember(tt.dates,H)
tt([row],:) = [];
end
end
This is my plan, but I am not sure if my syntax is correct because I am completely new to Matlab... I started today.
Please tell me what code I should write if my code is wrong.
Thank you!
0 Commenti
Risposte (2)
Berkay Sen
il 6 Giu 2019
Hi Juniper,
Firstly, convert table to matrix,
for i=1:%dates
if matrix(i,3)==0
matrix(i,:)=[];
i=i-1;
end
end
after that you can convert matrix to table
2 Commenti
Peter Perkins
il 7 Giu 2019
Converting to a matrix is making your life much harder. Use the native time subscripting built into timetables. See, for example, Steve Lord's answer.
Steven Lord
il 6 Giu 2019
Don't check each individual element of the timetable array's times individually. Let's make a simple timetable on which to operate.
dt = datetime('today', 'Format', 'eee MMM d') + days(0:6).';
data = (1:7).'.^2;
T = timetable(dt, (1:7).'.^2, 'VariableNames', {'squares'})
Let's identify all the weekdays.
theWeekdays = ~isweekend(T.dt)
Now we can split the timetable into two (well, I'm going to create two new ones so you can compare them with the original T), one for the weekdays and one the weekends, using the logical vector theWeekdays as a "mask".
justWeekdays = T(theWeekdays, :)
justWeekends = T(~theWeekdays, :)
Using logical indexing like this, all you need to do is write the code to create the logical mask. Since you're using the holidays function you have Financial Toolbox, which means you also have the isbusday function that should make this easy.
FYI if you look at the end of the documentation pages for a function most have a "See Also" section listing other functions that are related to that function, and isbusday is listed in the See Also for holidays. So this section can help you learn more about functions that may be useful for your application.
1 Commento
Peter Perkins
il 7 Giu 2019
And if you have a list of holidays or whatever that you want to remove, create a datetime vector, and use that as a subscript to delete rows, for example
T(datetime(2019,6,10),:) = []
Vedere anche
Categorie
Scopri di più su Calendar in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!