Reputation: 11
I'm a little rookie in Matlab and I met some trouble with my homework
I have Excel which looks like this :
Date column A column B
1/1/2015 (double) (double)
5/1/2015 (double) (double)
6/1/2015 (double) (double)
12/2/2015 (double) (double)
13/2/2015 (double) (double)
15/3/2015 (double) (double)
11/4/2015 (double) (double)
12/4/2015 (double) (double)
13/4/2015 (double) (double)
11/5/2015 (double) (double)
13/5/2015 (double) (double)
11/6/2015 (double) (double)
11/7/2015 (double) (double)
11/8/2015 (double) (double)
11/9/2015 (double) (double)
11/10/2015 (double) (double)
11/11/2015 (double) (double)
11/12/2015 (double) (double)
.
########(string?) ... ...
########(string?) ... ...
15/8/2018 ... ...
.
.
.
I imported and converted it into a table, and now I need to filter it and leave only the first day of each quarter.
since Matlab is so different from things like MySQL, java, things I learned before and I got not much time, I'm panic in this desperate situation(it just a start of my homework!), so I really need some help.
I'm not familiar with the syntax and this the first time I asked a question, sorry if I ask something stupid.
Upvotes: 0
Views: 167
Reputation: 11
table = readtable("a.csv");
table= table(month(table.Date)==4 | month(table.Date)==7 | month(table.Date)==10 | month(table.Date)==1, :);
just found a way that can filter specific month
now I just need to keep the first day of a month only and delete all other day
Upvotes: 1
Reputation: 1273
You can use the datetime class in Matlab to compare dates.
Assuming:
1. Your dates are in the format dd/MM/yyyy
2. Dates in column 1 are already sorted when printing out from Excel
3. The delimiter for the csv file titled data.csv
is a space,
the following snippet should print out the columns of the first occurring date of each quarter.
clc; clear;
fid = fopen('data.csv');
% Retrieve data from file
data = textscan(fid, '%s %s %s', 'delimiter', ' ');
% Convert string to date objects
% that enables date comparison
datesArray = datetime(data{1}, 'InputFormat', 'dd/MM/yyyy');
% Define start and end dates of each quarter
QstartDayMonth = ['01/01'; '01/04'; '01/07'; '01/10'];
QendDayMonth = ['31/03'; '30/06'; '30/09'; '31/12'];
% Parse data to extract seasonal firsts
quart = 1;
for i = 1:length(datesArray)
% Obtain year
currentDate = datesArray(i);
currentYear = num2str(currentDate.Year);
% Define start date of current year's quarter
quarterStart = strcat(QstartDayMonth(quart, :), '/', currentYear);
quarterEnd = strcat(QendDayMonth(quart, :) , '/', currentYear);
quarterStart = datetime(quarterStart, 'InputFormat', 'dd/MM/yyyy');
quarterEnd = datetime(quarterEnd, 'InputFormat', 'dd/MM/yyyy');
% Check if current date lies in chosen quarter
if (currentDate >= quarterStart)
disp([data{1}{i}, ' ', data{2}{i}, ' ', data{3}{i}]);
% If it does, advance the quarter to be checked
quart = quart + 1;
if (quart > 4)
quart = 1;
end
continue;
end
end
fclose(fid);
Upvotes: 0