ven_ni
ven_ni

Reputation: 1

data filtering in matlab

I am a beginner in matlab. I am doing a small project where i am facing issues regarding filtering I have a subset of my data below

'black' 11  '6/21/2013' <1x1 cell>
'blue'  11  '6/3/2013'  <1x1 cell>
'yellow'12  '4/18/2015' <1x1 cell>
'white' 13  '11/11/2013'<1x1 cell>
'red'   14  '8/4/2014'  <1x1 cell>
'blue'  15  '8/4/2014'  <1x1 cell>
'yellow'16  '12/6/2014' <1x1 cell>
'red'   17  '10/4/2014' <1x1 cell>
'red'   18  '4/17/2015' <1x2 cell>
green'  19  '12/14/2014'<1x1 cell>
orange' 20  '3/18/2015' <1x1 cell>

The last column of the data is a cell array which has some values in it which are as below :

<1x1 cell>  'b1'    
<1x1 cell>  'c1'    
<1x1 cell>  'b2'    
<1x1 cell>  'c1'    
<1x1 cell>  'b5'    
<1x1 cell>  'd2'    
<1x1 cell>  'f1'    
<1x1 cell>  'f1'    
<1x2 cell>  'b2'    'c1'
<1x1 cell>  'c1'    
<1x1 cell>  'c1'    

I want to count the number of occurrences of b1,c1,f1 (the entire data set has values from a to z, a1 to z1 and so on) to between two dates chosen by the user and have the output as

from date:
to date: 

      count
f1 
b1
c1

thankyou for the help.

Upvotes: 0

Views: 140

Answers (1)

Adriaan
Adriaan

Reputation: 18177

StartDate = input('What is the start date ','s'); % Prompt user for start date
EndDate = input('What is the end date ','s'); % Prompt user for end date
StartDate = datenum(StartDate); % Convert to datenum
EndDate = datenum(EndDate);

a=0;b=0;c=0; % Initalise counters
alphabet = {'a', 'b', 'c', ....} %EXPAND THIS TO Z
lettercount = zeros(1,numel(alphabet));
for ii = 1:size(YourCell,1)
   celldat = datenum(YourCell{ii,3}); % Get date 
   if celldat > StartDate && celldat < EndDate % Check time boundaries
     for jj = 1:size(YourCell{ii,4})
         tmp = strcmp(YourCell{ii,4}{jj},alphabet);
         lettercount = lettercount + tmp;
     end
   end
end

What happens here is that you first prompt the user for the start and end dates of your time interval. You then convert all dates to datenum for easy comparison. The counters are initialised and then for each cell the date is checked against the prompted dates, and, if within the time period, the letters are collected in lettercount, where lettercount(1)=a, lettercount(2)=b, etc

Upvotes: 0

Related Questions