MATLAB Answers

0

Adding column values to a cell

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
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
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.

サインイン to comment.

タグ

1 件の回答

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 件のコメント

jonas
2018 年 9 月 5 日
This will give you the 3rd column
obs=~contains(data_2(:,1),'-');
A=nan(size(data_2,1),1)
A(~obs)=1:sum(~obs)
A=fillmissing(A,'previous')
...I still feel mixing dates and numbers in the same column is a bit inconvenient.
Guillaume
2018 年 9 月 5 日
Or simpler:
A = cumsum(contains(data_2(:, 1), '-'))
Hari krishnan 2018 年 9 月 5 日
Thank you. This worked well.

サインイン to comment.



Translated by