Extract data in a table following a a range of date (years)
조회 수: 14 (최근 30일)
이전 댓글 표시
Good day to all,
I have a thre column table, after some manipulation I get the years from dd/mm/yyyy to only yyyy. Now I have to filter this table using year as filter variable.
Date starts at 1900 and ends at 2050, the interval i need is from 1956 to 2020.
I have created a logical vector using
yearFiltered=newDataFiltered.yearFiltered(newDataFiltered.yearFiltered>1955 & newDataFiltered.yearFiltered<2021)
then i've tried to index the main data set using the variable above following A(B)=[]
the outcome of the syntax is that "when deleting elements from a table variable using indexed assignments, the number of the rows must not change. Specify the first subscript as a colon (:) and exactly one other subscript that is not a colon"
infact the rows number will be reduced.
댓글 수: 0
채택된 답변
Cris LaPierre
2022년 11월 18일
Your table has 3 columns. Your deletion code must delete all 3 columns. You specify 'all columns' by adding a colon in the 2nd position.
A(B,:)=[]
% ^^ add colun to second index position
댓글 수: 2
Cris LaPierre
2022년 11월 18일
Use a logical expression to identify the rows within the range of years you want, and then either extract those rows, all columns to a new variable or delete all other rows, all columns by taking the NOT of your logical expression.
a = ([1:3;4:6])'
idx = a(:,1)==2
% Extract row
b = a(idx,:)
% delete other rows
a(~idx,:)=[]
추가 답변 (1개)
Campion Loong
2022년 11월 18일
편집: Campion Loong
2022년 11월 21일
I would use timetable for this kind of operations. It is specifically built for time based workflows.
% Mock data for time between 1900 - 2050
dt = datetime(1900,1,1)+calmonths(1:12*150)';
data = (1:length(dt))';
% timetable automatically picks up the one datetime variable as your time vector
tt = timetable(dt, data)
% Make a timerange subscript for 1956 - 2020
tr = timerange(datetime(1956,1,1),datetime(2020,12,31))
% Get all the rows within this range of time
tt(tr, :)
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!