Extract data in a table following a a range of date (years)
    13 views (last 30 days)
  
       Show older comments
    
    alejandro paradiso
 on 18 Nov 2022
  
    
    
    
    
    Edited: Campion Loong
    
 on 21 Nov 2022
            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 Comments
Accepted Answer
  Cris LaPierre
    
      
 on 18 Nov 2022
        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 Comments
  Cris LaPierre
    
      
 on 18 Nov 2022
				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,:)=[]
More Answers (1)
  Campion Loong
    
 on 18 Nov 2022
        
      Edited: Campion Loong
    
 on 21 Nov 2022
  
      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 Comments
See Also
Categories
				Find more on Calendar in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!