How to filter rows from excel in matlab
10 次查看(过去 30 天)
显示 更早的评论
Hi, I am working on a project and I need this small help. I am currently observing the average speed for cars during weekdays and weekends. Now I have an excel sheet and imported all of the data as column vectors, observation number, time, date, day of the week, average speed of a car, that sort of thing. Now here is where I am stuck. I need to classify or lets say filter all the observations as either weekday or weekend refer to the screenshot for how the excel looks like. so how can I classify the observation as a weekday or weekend![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1684681/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1684681/image.jpeg)
0 个评论
回答(1 个)
Star Strider
2024-5-2
编辑:Star Strider
2024-5-2
There are two principal options.
D = '12 May 2024';
[Dnum,Dnam] = weekday(D)
D = datetime([2024 05 12])
Dnam = day(D, 'name')
Dnam = day(D, 'shortname')
Dnum = day(D,'dayofweek')
You can then use the day numbers to classify the days as either weekdays (2 to 6) or weekends (7 or 1).
.
EDIT — Corrected typographical errors.
2 个评论
Star Strider
2024-5-2
编辑:Star Strider
2024-5-2
It would help to have the file.
Since there is aready a ‘DayOf Week’ column (that I didn’t see before, since that image is vanishingly small), one option (where ‘Table’ is the name for the table created by reading the file, change that as necessary) could be:
Table.DayOfWeek = {'Sunday'; 'Monday';'Tuesday';'Friday';'Saturday'};
daytypes = ismember(Table.DayOfWeek, {'Saturday','Sunday'})
Weekends = table(Table.DayOfWeek, daytypes, 'VariableNames',{'DayOfWeek','WeekendDay'})
This returns ‘true’ (or 1) for days that are weekends and 'false' (or 0) for weekdays. You can use that to create your categories.
.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!