Arranging corresponding elements of rows in a cell.
1 view (last 30 days)
Show older comments
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 Comments
jonas
on 14 Aug 2018
Got it. So in the end your output should have the same amount of rows as the number of detections?
Accepted Answer
jonas
on 14 Aug 2018
Edited: jonas
on 14 Aug 2018
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 Comments
More Answers (1)
See Also
Categories
Find more on Dates and Time 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!