Adding column values to a cell

1 Ansicht (letzte 30 Tage)
Hari krishnan
Hari krishnan am 5 Sep. 2018
Kommentiert: Hari krishnan am 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 Kommentare
jonas
jonas am 5 Sep. 2018
Where does this data come from? It's not very difficult to obtain the results you want, but perhaps it's even easier to fix this issue already in the data import.
Guillaume
Guillaume am 5 Sep. 2018
Bearbeitet: Guillaume am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

jonas
jonas am 5 Sep. 2018
Bearbeitet: jonas am 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 Kommentare
Guillaume
Guillaume am 5 Sep. 2018
Or simpler:
A = cumsum(contains(data_2(:, 1), '-'))
Hari krishnan
Hari krishnan am 5 Sep. 2018
Thank you. This worked well.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by