Arranging corresponding elements of rows in a cell.
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
Jan
on 14 Aug 2018
I do not understand, what the problem is.
Just a hint:
if isempty(strfind(data{kk}(ii,1),'-'))==0
could be expressed simpler:
if contains(data{kk}(ii,1), '-')
or
if any(data{kk}(ii,1) == '-')
Hari krishnan
on 14 Aug 2018
jonas
on 14 Aug 2018
Should be fairly easy. Can you give upload some sample data?
Hari krishnan
on 14 Aug 2018
Hari krishnan
on 14 Aug 2018
jonas
on 14 Aug 2018
What happened to the image that you had in your original question? The structure in that image was completely different... In this .txt all dates are associated with multiple values.
Hari krishnan
on 14 Aug 2018
As you described it before, the data structure was very clear. You had a bunch of dates, and after certain dates you had data entries corresponding to a detection. You wanted to find the data and the associated time-stamp.
In the file you attached, you have multiple detections (multiple rows of data) for each time-stamp... In terms of coding, this is an entirely different problem. What is your desired output anyway?
Hari krishnan
on 14 Aug 2018
So basically you want to have a bunch of dates (rounded to seconds) and their corresponding detection ID's? Something like this?
date1 'id1 id2 id3...'
date2 'empty'
date3 'empty'
date4 'id1 id2'
where id is the first value of the detection, e.g. 1329
Hari krishnan
on 14 Aug 2018
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?
Hari krishnan
on 14 Aug 2018
Accepted Answer
More Answers (1)
Hari krishnan
on 14 Aug 2018
8 Comments
jonas
on 14 Aug 2018
That is really weird... exactly the same output as me. Try with this inputformat
Dates=datetime(C{1}(DateLocs),'inputformat','yyyy-MM-dd HH:mm:ss.SSSSSS');
Hari krishnan
on 14 Aug 2018
jonas
on 14 Aug 2018
Great! I hope it works with the full dataset as well. Otherwise I have made an alternative solution that may work.
Hari krishnan
on 14 Aug 2018
Edited: Hari krishnan
on 14 Aug 2018
jonas
on 14 Aug 2018
OK! Back to work.
jonas
on 14 Aug 2018
It almost works. Do you want an empty date with NaN in the table if there is no detection data? Or just remove the date completely?
Hari krishnan
on 14 Aug 2018
jonas
on 14 Aug 2018
Great!
Categories
Find more on Multidimensional Arrays 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!