How to separate weekends from weekdays from an Excel file in MATLAB?

조회 수: 3 (최근 30일)
Wolfgang McCormack
Wolfgang McCormack 2021년 1월 18일
편집: dpb 2021년 1월 18일
Hi Everone,
I have an Excel file like this:
(the date is like a text file)
Column 1 Column 2 Column 3
2020-04-02-thursday 45 67
2020-04-03-friday 33 88
....
How can I seperate the weekends and weekdays and write them to two seperate files in MATLAB?
Also, how can I extract a range from these days? For example, extract the days that their column 2 has value above 60?
Also, another question, how can I say, multiply cells that have a value above 50 by 2 in columns2 and multiply cells that have a value betwenn 20 and 30 by 0.5 in column 2.
Thank you so much. I am a novice in MATLAB so I would really appreciate it if you could help me!

채택된 답변

dpb
dpb 2021년 1월 18일
Well, ya' gotsa' start somewhere! We all were newbies once...give it a shot and see how far you get on your own first...
  1. Import the data with readtable or since you do have dates, optionally readtimetable
  2. See weekday combined with
  3. logical indexing; one of the most powerful features in MATLAB and one must learn to use to be effective.
All of the above is pretty simple and straightforward but you'll learn MATLAB faster by trying than just taking code somebody else hands you.
The outline of the above would be something like
t=readtimetable('ExcelFile.xlsx');
isWeekEnd=beginsWith(t.Date,'S','ignorecase',1); % logical vector if day starts with S|s
tWE=t(isWeekEnd,:); % the WE only table all columns
ix=t.Var2>50; % logical vector Var2 > 50
t.Var2(ix)=2*t.Var2(ix); % multiply those by 2
As can see, it's all pretty obvious when see it...there are examples of all this syntax in the "getting started" section in the doc or examples for the various functions.
  댓글 수: 6
Wolfgang McCormack
Wolfgang McCormack 2021년 1월 18일
@dpb thank you so much for the info.
Regarding the time range, I actually looked it up but it includes the dates as well. I need to select the hours and then select dates using another timerange. But it's not working the way i want :D
dpb
dpb 2021년 1월 18일
Well, I'm not sure about how to do that with timerange as I've not used it enough to be fully familiar with the optional use of durations and the 'unitOfTime' time periods to be able to do this internally to it or not. The examples are of some help but do not always explore all the nuances that are possible.
However, there was this link to what appears to be the same question on the Answers page <How-to-filter-rows-in-a-timetable-based-on-a-range-between-08-00-and-16-00-for-everyday> that appears to address the Q?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by