Iterating through Matlab Table
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a simple Matlab table with dates and 2 additional columns that contain numbers and strings. The dates are NOT daily, that is sometimes multple days are missing.
I would like to forward fill the table such that the table becomes a daily table and for each day that is missing, the rows from the previous date are copied.
댓글 수: 0
채택된 답변
KSSV
2022년 2월 24일
LEt T be your table. With first column as dates.
% First make the dates daily
thedates = T.(1) ; % existing dates of the table
thedates_new = (thedates(1):thedates(2))' ; % Make daily dates
% GEt the existing dates indices
[idx,ia] = ismember(thedates_new,thedates) ;
C2 = T.(2) ; % second column of table
C2_new = NaN(size(thedates_new)) ;
C2_new(idx) = C2;
C2_new = fillmissing(C2_new) ;
T_new = table(thedates_new,C2_new) ;
Alternatively you may have a look on the function retime
추가 답변 (2개)
Seth Furman
2022년 2월 25일
timetable has a retime method that supports resampling by copying the previous value.
tt = timetable(datetime(2020,1,[1;3;4;7;9]), [1;3;4;7;9])
retime(tt, "daily", "previous")
Arif Hoq
2022년 2월 24일
date = {'2014-05-20';'2014-05-21';'2014-05-22';'2014-05-24';'2014-05-25'}; % define the date
name = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'}; % define the string
age = [38;43;38;40;49]; % define the numerical
T=table(date,name,age) % make a table
idx = diff(datenum(T.date, 'yyyy-mm-dd')).'==1; % return the missing date
idx2=find(idx==0); % missing date index
newdate=[T.date(1:idx2);T.date(idx2);T.date(idx2+1:end)]; % concatenate the date
newname=[T.name(1:idx2);T.name(idx2);T.name(idx2+1:end)]; % concatenate the name
newage=[T.age(1:idx2);T.age(idx2);T.age(idx2+1:end)]; % concatenate the age
newTable=table(newdate,newname,newage) % newtable
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!