Arranging corresponding elements of rows in a cell.

1 回表示 (過去 30 日間)
Hari krishnan
Hari krishnan 2018 年 8 月 14 日
コメント済み: jonas 2018 年 8 月 14 日
I have a data file as attached . The first column corresponds to date, second column corresponds to time. Whenever an object is detected at a particular time, the identity of the object along with with the X, Y coordinates are added to the next row.
What i am trying do is to calculate the time in seconds and keep the identity of corresponding detection and coordinates together in a single row. I wrote a code for this, but it is having a problem with matching the time associated with detection. It associates the time stamp of first detection rather than the actual time to the corresponding coordinates.
for ii=1:data_size{kk}(1)
if isempty(strfind(data{kk}(ii,1),'-'))==0; %for checking if a specific pattern '-' is in the rows of the first column nad if its there, then calculate the time in seconds
[~,~,~,H,MN,S] = datevec(strcat(data{kk}(ii,1)," ", data{kk}(ii,2))); %here it takes the year, month, day, hour,minutes,sec
start_time = S+60*MN+3600*H; %here everything is converted to seconds
else
ant_trajectories{kk}(dex{kk},1) = start_time; %first column have the time in sec when tag is detected
ant_trajectories{kk}(dex{kk},2) = data{kk}(dex{kk},1); %takes the id and saves it to second column
ant_trajectories{kk}(dex{kk},3) = data{kk}(dex{kk},2); %takes the X coordinate and saves it in the third column
ant_trajectories{kk}(dex{kk},4) = data{kk}(dex{kk},3); %takes the Y coordinate and saves it in the fourth column
dex{kk}=dex{kk}+1;
end
end
  14 件のコメント
jonas
jonas 2018 年 8 月 14 日
Got it. So in the end your output should have the same amount of rows as the number of detections?
Hari krishnan
Hari krishnan 2018 年 8 月 14 日
Yes. Exactly. There will be some time stamps where i donot have any detections. What i just want to do is the associate this with time.

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

採用された回答

jonas
jonas 2018 年 8 月 14 日
編集済み: jonas 2018 年 8 月 14 日
Thsi code got ugly quick. Anyway, here's something I stitched together. I'm not sure if it will work when there is nothing detected.
%%Read dates and the two second rows of data
opt = {'Delimiter','(),\t','MultipleDelimsAsOne',true,'CollectOutput',true};
fmt = ['%s%f%f %*[^\n]']
[fid,msg] = fopen('daaata.txt','rt');
C = textscan(fid,fmt,opt{:});
fclose(fid);
%%Grab the first column of data...
[fid,msg] = fopen('daaata.txt','rt');
fmt = ['%f %*[^\n]']
D = textscan(fid,fmt,opt{:});
fclose(fid);
%%Find index of dates
DateLocs=find(isnan(C{2}(:,1))==1);
%%Convert to DateTime
Dates=datetime(C{1}(DateLocs));
%%Create time-vector
t(DateLocs)=Dates'
%%Remove NaNs from data
data=[C{2} D{1}];
%%Add date at end of series and fillmissing
t(size(data,1))=Dates(end)
t=fillmissing(t,'previous')'
%%Store in TimeTable
TT=timetable(t,data)
%%Delete rows with NAN
TT(isnan(TT.data(:,1)),:)=[];
TT =
58×1 timetable
t data
____________________ __________________________
17-May-2018 13:00:02 1703.7 1479.3 1329
17-May-2018 13:00:02 1644.7 1468.1 1326
17-May-2018 13:00:02 1585.6 1456.9 1324
17-May-2018 13:00:02 1526.6 1445.7 1321
17-May-2018 13:00:02 1606.2 596.35 1220
17-May-2018 13:00:02 1665.8 590.81 1212
17-May-2018 13:00:02 1725.6 585.5 1211
17-May-2018 13:00:02 1785.2 580.38 1209
17-May-2018 13:00:02 3106.8 327.73 1885
17-May-2018 13:00:07 1703.7 1479.3 1329
...
I don't understand what you meant by converting to seconds but I am sure you can easily do it now that you have the data in DateTime format.
  5 件のコメント
Hari krishnan
Hari krishnan 2018 年 8 月 14 日
I ran with the same file shown in the question
jonas
jonas 2018 年 8 月 14 日
OK! Show me what
C{1}(DateLocs)
outs

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

その他の回答 (1 件)

Hari krishnan
Hari krishnan 2018 年 8 月 14 日
{'2018-05-17 13:00:02.754000'}
{'2018-05-17 13:00:07.047000'}
{'2018-05-17 13:00:11.279000'}
{'2018-05-17 13:00:15.571000'}
{'2018-05-17 13:00:19.864000'}
{'2018-05-17 13:00:24.098000'}
{'2018-05-17 13:00:28.389000'}
  8 件のコメント
Hari krishnan
Hari krishnan 2018 年 8 月 14 日
Hi, it works. It was a mistake i was making. Thank you very much :)
jonas
jonas 2018 年 8 月 14 日
Great!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by