Adding column values to a cell
조회 수: 2 (최근 30일)
이전 댓글 표시
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일
편집: 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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!