필터 지우기
필터 지우기

How to filter rows from excel in matlab

조회 수: 10 (최근 30일)
Nour Helal
Nour Helal 2024년 5월 2일
편집: Star Strider 2024년 5월 2일
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

답변 (1개)

Star Strider
Star Strider 2024년 5월 2일
편집: Star Strider 2024년 5월 2일
There are two principal options.
If you are only importing the dates as strings, use the weekday function —
D = '12 May 2024';
[Dnum,Dnam] = weekday(D)
Dnum = 1
Dnam = 'Sun'
For datetime variables, use the day function —
D = datetime([2024 05 12])
D = datetime
12-May-2024
Dnam = day(D, 'name')
Dnam = 1x1 cell array
{'Sunday'}
Dnam = day(D, 'shortname')
Dnam = 1x1 cell array
{'Sun'}
Dnum = day(D,'dayofweek')
Dnum = 1
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
Nour Helal
Nour Helal 2024년 5월 2일
thanks for ther assist im only confused with this one part, how am I gonna have the code refer to the excel file. I imported the excel file as a string array. so what can I do
Star Strider
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'})
daytypes = 5x1 logical array
1 0 0 0 1
Weekends = table(Table.DayOfWeek, daytypes, 'VariableNames',{'DayOfWeek','WeekendDay'})
Weekends = 5x2 table
DayOfWeek WeekendDay ____________ __________ {'Sunday' } true {'Monday' } false {'Tuesday' } false {'Friday' } false {'Saturday'} true
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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by