Extract data in a table following a a range of date (years)

9 ビュー (過去 30 日間)
alejandro paradiso
alejandro paradiso 2022 年 11 月 18 日
編集済み: Campion Loong 2022 年 11 月 21 日
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.

採用された回答

Cris LaPierre
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 件のコメント
alejandro paradiso
alejandro paradiso 2022 年 11 月 18 日
thank you cris,
infact I want also erase the rows from the other columns
'Monday' '08:07:30 PM' 1900
'Wednesday' '02:50:12 PM' 1910
'Friday' '09:11:48 AM' 1920
'Sunday' '02:02:06 AM' 1930
'Monday' '04:36:36 PM' 1940
'Wednesday' '04:38:36 AM' 1950
'Thursday' '02:21:54 PM' 1900
from the data above, lets say i'm interested (columns & rows) in the years from 1920 to 1940, so in the end i will have a table 3x3
Cris LaPierre
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])'
a = 3×2
1 4 2 5 3 6
idx = a(:,1)==2
idx = 3×1 logical array
0 1 0
% Extract row
b = a(idx,:)
b = 1×2
2 5
% delete other rows
a(~idx,:)=[]
a = 1×2
2 5

サインインしてコメントする。

その他の回答 (1 件)

Campion Loong
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)
tt = 1800×1 timetable
dt data ___________ ____ 01-Feb-1900 1 01-Mar-1900 2 01-Apr-1900 3 01-May-1900 4 01-Jun-1900 5 01-Jul-1900 6 01-Aug-1900 7 01-Sep-1900 8 01-Oct-1900 9 01-Nov-1900 10 01-Dec-1900 11 01-Jan-1901 12 01-Feb-1901 13 01-Mar-1901 14 01-Apr-1901 15 01-May-1901 16
Then use timerange to select data you are interested in
% Make a timerange subscript for 1956 - 2020
tr = timerange(datetime(1956,1,1),datetime(2020,12,31))
tr =
timetable timerange subscript: Select timetable rows with times in the half-open interval: [01-Jan-1956 00:00:00, 31-Dec-2020 00:00:00) See Select Times in Timetable.
% Get all the rows within this range of time
tt(tr, :)
ans = 780×1 timetable
dt data ___________ ____ 01-Jan-1956 672 01-Feb-1956 673 01-Mar-1956 674 01-Apr-1956 675 01-May-1956 676 01-Jun-1956 677 01-Jul-1956 678 01-Aug-1956 679 01-Sep-1956 680 01-Oct-1956 681 01-Nov-1956 682 01-Dec-1956 683 01-Jan-1957 684 01-Feb-1957 685 01-Mar-1957 686 01-Apr-1957 687
  2 件のコメント
alejandro paradiso
alejandro paradiso 2022 年 11 月 19 日
interesting suggestion, thank you Campion
Campion Loong
Campion Loong 2022 年 11 月 21 日
You're welcome.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by