Extract data in a table following a a range of date (years)
9 ビュー (過去 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 で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!