Adding column values to a cell

1 view (last 30 days)
Hari krishnan
Hari krishnan on 5 Sep 2018
Commented: Hari krishnan on 5 Sep 2018
Hi,'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 the frame number corresponding to each time stamp. In the sample code shown, i am adding the frame number to the first column. I wrote a sample code to perform this. When i perform this, the increasing frame number is 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];
  2 Comments
Guillaume
Guillaume on 5 Sep 2018
I agree with Jonas, it is probably better to fix the import if possible.
Also note, that
1:size(contains(data_2(:,1),'-')))
probably doesn't do what you want. For a start 1:size(something) only works properly if something is a column vector and what is really meant is 1:numel(something). Secondly the size of the vector returned by contains is always going to be the same size as the input whether or not any row contains the desired search string. So in effect, your 1:size(...) is exactly the same as:
1:size(data_2, 1)
Finally, I would recommed against converting numbers to char arrays or strings. Keep them as numbers, it's easier to work with and is faster.

Sign in to comment.

Accepted Answer

jonas
jonas on 5 Sep 2018
Edited: jonas on 5 Sep 2018
You could do something like this:
% find segments of objects
obs=~contains(data_2(:,1),'-');
locs=find([diff([0;obs;0])]~=0);
starts=locs(1:2:end);
ends=locs(2:2:end)-1;
% new cell array
A=cell(length(data_2),3);
A(:,1:2)=data_2(:,1:2);
for i=1:length(starts)
A(starts(i)-1,3)={data_2(starts(i):ends(i),1)}
end
A(obs,:)=[]
A =
8×3 cell array
{'2018-03-11'} {'15:28:30'} {0×0 double}
{'2018-03-11'} {'15:28:32'} {0×0 double}
{'2018-03-11'} {'15:28:34'} {0×0 double}
{'2018-03-11'} {'15:28:36'} {0×0 double}
{'2018-03-11'} {'15:28:38'} {2×1 cell }
{'2018-03-11'} {'15:28:40'} {0×0 double}
{'2018-03-11'} {'15:28:42'} {0×0 double}
{'2018-03-11'} {'15:28:44'} {1×1 cell }
Now you have one row for each frame with every object stored in the 3rd column.
  6 Comments
Hari krishnan
Hari krishnan on 5 Sep 2018
Thank you. This worked well.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by