필터 지우기
필터 지우기

Index through datestring, selecting whole hours

조회 수: 2 (최근 30일)
Stefan Hristov
Stefan Hristov 2014년 10월 3일
답변: Stefan Hristov 2014년 10월 3일
Hi,
i have the following problem. I have created a cell array consisting of two columns date and value, which looks like this (extract):
'02-10-2014 20:30' [10934]
'02-10-2014 21:00' [10195]
'02-10-2014 21:30' [ 9388]
'02-10-2014 22:00' [ 8808]
'02-10-2014 22:30' [ 8730]
I want to filter through the array and select only values that correspond to whole hours like:
'02-10-2014 21:00' [10195]
'02-10-2014 22:00' [ 8808]
Any suggestion please?
Many thanks
Stefan

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 10월 3일
편집: Azzi Abdelmalek 2014년 10월 3일
a={'02-10-2014 20:30' [10934]
'02-10-2014 21:00' [10195]
'02-10-2014 21:30' [ 9388]
'02-10-2014 22:00' [ 8808]
'02-10-2014 22:30' [ 8730]}
c1=datevec(a(:,1),'dd-mm-yyyy HH:MM')
idx=c1(:,5)==0
out=a(idx,:)
  댓글 수: 1
Stefan Hristov
Stefan Hristov 2014년 10월 3일
Many thanks Azzi,
that worked just fine! Actually i tried just datevec(a(;,1)) withoug assigning the format and it worked fine as well.
Best
Stefan

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Image Analyst
Image Analyst 2014년 10월 3일
Something like
counter = 1;
for row = 1 : size(ca, 1)
cellContents = ca{row, 1}; % Extract first column of this row.
if strfind(cellContents, ':00')
% Cell contains :00, so append it to the output cell array.
output_ca{counter, 1} = cellContents;
output_ca(counter, 2) = ca(row, 2);
counter = counter + 1;
end
end
That's one way at least, sort of the intuitive but brute force way. There are probably other ways that are more compact, like using cellfunc() or something. Note how I used braces and parentheses. If you don't know why, see the FAQ : http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F. You may find it easier to use a structure array than a cell array.

Andrei Bobrov
Andrei Bobrov 2014년 10월 3일
편집: Andrei Bobrov 2014년 10월 3일
Z = {'02-10-2014 20:30' [10934]
'02-10-2014 21:00' [10195]
'02-10-2014 21:30' [ 9388]
'02-10-2014 22:00' [ 8808]
'02-10-2014 22:30' [ 8730]};
[~,~,~,~,m] = datevec(Z(:,1),'dd-mm-yyyy HH:MM');
out = Z(m == 0,:);
or
out = Z(~cellfun('isempty',regexp(Z(:,1),':00')),:);

Matz Johansson Bergström
Matz Johansson Bergström 2014년 10월 3일
편집: Matz Johansson Bergström 2014년 10월 3일
Another alternative where I just pick the last two characters from the first column of the cell:
%mock data:
cel{1,1} = '02-10-2014 21:00'; cel{1,2} = 3.2;
cel{2,1} = '02-10-2014 21:03'; cel{2,2} = 7.2;
mask = cellfun(@(x) strcmp(x(end-1:end), '00'), cel(:,1))
>> cel(mask,:)
ans =
'02-10-2014 21:00' [3.2000]
If you want to find out how many entries that are on the hour:
sum(mask)

Stefan Hristov
Stefan Hristov 2014년 10월 3일
Hi all,
many thanks for your answers! I wasn't expecting that many answers so fast. It's great to see such enthusiasm in the forum as i am quite new to Matlab.
I am going through all the solutions and trying to learn. :)
Best!
Stefan

Community Treasure Hunt

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

Start Hunting!

Translated by