Adding column values to a cell

1 回表示 (過去 30 日間)
Hari krishnan
Hari krishnan 2018 年 9 月 5 日
コメント済み: Hari krishnan 2018 年 9 月 5 日
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 件のコメント
jonas
jonas 2018 年 9 月 5 日
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 2018 年 9 月 5 日
編集済み: Guillaume 2018 年 9 月 5 日
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.

サインインしてコメントする。

採用された回答

jonas
jonas 2018 年 9 月 5 日
編集済み: jonas 2018 年 9 月 5 日
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 件のコメント
Guillaume
Guillaume 2018 年 9 月 5 日
Or simpler:
A = cumsum(contains(data_2(:, 1), '-'))
Hari krishnan
Hari krishnan 2018 年 9 月 5 日
Thank you. This worked well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by