extracting rows based on string in first column (Should be fairly simple ... not for me apparently)
조회 수: 3 (최근 30일)
이전 댓글 표시
Hey everyone,
I have a large cell array with 8 columns (8758 x 8 cell). The first containing time in the string format of '2010-01-01 00:00'. I am trying to extract all the rows where the timestamp is '01' at the 9th and 10th characters.
I know this is relatively basic but cannot get the indexing right. I had this so far:
rows = find(a{:,1}(9,10) == '01')
extrdata = a{rows,1};
I have also tried the following but get the following error:
>> a( a{:,1}(9:10) == '01' , : )
Bad cell reference operation.
If I were to index just for one row then it works but my indexing is wrong to work for all rows:
>> a{1,1}(9:10)
ans =
01
Thanks for any help!
댓글 수: 0
채택된 답변
per isakson
2014년 4월 23일
편집: per isakson
2014년 4월 23일
Problem: extracting first day of each month from one year of hourly data (two missing hours). One alternative is to compare day number
%%Create sample data
vec = datevec( '2010-01-01 00:00', 'yyyy-mm-dd HH:MM' );
num = datenum( vec(1),vec(2),vec(3),[vec(4)+transpose([0:8757])],0,0 );
str = datestr( num, 'yyyy-mm-dd HH:MM' );
cac = cat( 2, num2cell( str, 2 ), num2cell( ones(8758,7) ) );
%%find first day of each month
vec = datevec( cac(:,1), 'yyyy-mm-dd HH:MM' );
is1 = vec(:,3)==1;
ix1 = find( is1 );
%%extract rows
selection = cac( is1, : );
or comparing characters
str = char( cac(:,1) );
cel = num2cell( str( :, 9:10 ), 2 );
is1 = strcmp( '01', cel );
IMO: Neither of these two solutions is simple
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!