Search code examples
matlabvectorcell

Adding indices for columns


data_2 is a cell containing date in the first column and time in the second column. Whenever there is an object detected under a time stamp, the identity of the object along with the error is added to the next row. (This data is from video recordings of toy cars).

What i want to do is to add increasing index number corresponding to each time stamp. In the sample code shown, i am adding the index to the first column. I wrote a sample code to perform this. When i perform this, the increasing index is also added for detections under a single time stamp.

Eg: If you look at row 5 there are two detections and the identities are added to the rows 6 and 7. These both are detected at same time. But there frame number is not the same. It should be the same as of the time stamp, ie 5. Any help to solve this will be appreciated.

 data_2 = {'2018-03-11','15:28:30';'2018-03-11','15:28:32';'2018-03-11','15:28:34';'2018-03-11','15:28:36';'2018-03-11','15:28:38';'27','0';'29','1';'2018-03-11','15:28:40';'2018-03-11','15:28:42';'2018-03-11','15:28:44';'89','2'};
 frame_num_2 = strsplit(num2str(1:size(contains(data_2(:,1),'-'))))';   
 data_2 = [frame_num_2 data_2];

Solution

  • If I'm understanding your problem correctly, you want the frame number to increase by one only when there is a new date or time, and not when you have an object detected. To address this, you need to detect when a row is a date or time. Looking at your data, what is distinct about the date column is the presence of '-', and what is distinct about the time column is the presence of ':'. You can use either or both of these, I chose to use only the date column.

    data_date = data_2(:,1);
    data_time = data_2(:,2);
    %data that doesn't have a new frame contains a '-' symbol.  
    newFrame = contains(data_date, '-');
    frame_num = cumsum(newFrame); %only increase when there is a new frame
    Data = [num2cell(frame_num) data_2] %save in a new variable name
    

    which results in Data =

    {[1]}    {'2018-03-11'}    {'15:28:30'}
    {[2]}    {'2018-03-11'}    {'15:28:32'}
    {[3]}    {'2018-03-11'}    {'15:28:34'}
    {[4]}    {'2018-03-11'}    {'15:28:36'}
    {[5]}    {'2018-03-11'}    {'15:28:38'}
    {[5]}    {'27'        }    {'0'       }
    {[5]}    {'29'        }    {'1'       }
    {[6]}    {'2018-03-11'}    {'15:28:40'}
    {[7]}    {'2018-03-11'}    {'15:28:42'}
    {[8]}    {'2018-03-11'}    {'15:28:44'}
    {[8]}    {'89'        }    {'2'       }