필터 지우기
필터 지우기

Subtractions between consecutive rows in a table

조회 수: 5 (최근 30일)
DEYIRE UMAR
DEYIRE UMAR 2018년 9월 24일
답변: Peter Perkins 2018년 10월 1일
I need a help to obtain the differences between time values which I read alongside other data and write them to a CSV file as a table. The time values are those of MATLAB time converted into milliseconds, but what I need is the differences between them to be in the table as the time is read. That is row 3 - row2, row4 - row3, row5 - row4,.......rowend - row(end-1). I attached a CSV file where I read 30 samples. Here is the relevant segment of my code:
counter = 0;
logtime_table = table();
while(counter< 30)
Timestamp = now;
timestring = datestr(Timestamp,'HH:MM:SS.FFF');
[~,~,~,hours,minutes,seconds] = datevec(timestring);
Timestamp = 1000*(3600*hours + 60*minutes + seconds);
table_row = table(Timestamp);
logtime_table = [logtime_table; table_row];
counter = counter + 1;
end
filename = strcat('logtime_table.csv');
writetable(logtime_table, filename);

채택된 답변

Peter Perkins
Peter Perkins 2018년 10월 1일
Deyire, unless you're using a pretty old version of MATLAB, use datetime and duration instead of datestr/datenum/datevec. Also your code doesn't look like it computes time differences, it computes milliseconds since year zero. Try this:
>> t0 = datetime('now'); pause(rand); t1 = datetime('now'); >> milliseconds(t1 - t0) ans = 453.54
I suggest that in your loop, you save all the timestamps, as datetimes, and all the data at each timestamp, and then ONCE, outside the loop, create a timetable something like this:
dt = diff(timestamps); dt.Format = 's';
tt = timetable(data(2:end),'RowTimes',dt)

추가 답변 (1개)

KSSV
KSSV 2018년 9월 24일
Read about function diff. It will give you the required difference.
  댓글 수: 2
DEYIRE UMAR
DEYIRE UMAR 2018년 9월 27일
This function seem not to solve my problem. Does it work on tables at all?
KSSV
KSSV 2018년 9월 27일
It will.....you need to enter doubles into it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by