extracting rows based on string in first column (Should be fairly simple ... not for me apparently)

3 views (last 30 days)
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!

Accepted Answer

per isakson
per isakson on 23 Apr 2014
Edited: per isakson on 23 Apr 2014
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!