How to delete a rows in a timetable for the rows (dates) that are not trading days?

23 次查看(过去 30 天)
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:
  1. 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!

回答(2 个)

Berkay Sen
Berkay Sen 2019-6-6
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 个评论
Juniper Sohn
Juniper Sohn 2019-6-6
编辑:Juniper Sohn 2019-6-6
Is that the actual cocde?
Also, can you please write the code for converting the timetable to matrix as well?
My time stamp is exactly the format that I showed in my question at the top.
Also, what is the purpose of converting to and from matrix? And what is the code for checking if the dates are nontrading days or not --- and does this come after converting the timetable to matrix or somewhere else?
Thank you!
Peter Perkins
Peter Perkins 2019-6-7
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
Steven Lord 2019-6-6
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 个评论
Peter Perkins
Peter Perkins 2019-6-7
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),:) = []

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by