Matlab Date Frequency Statistics

Source: Internet
Author: User

I. Frequency statistics of pregnant women during the month of filing

Source data sample, for a hospital for a period of time for pregnant women to build the file time

2015-04-22 10:12:52

Objective: To count the number of pregnant women in each month, this is to extract the first column of the source data, with the interception date data, and then do the frequency histogram, to see whether the frequency of the establishment of pregnant women with the change of the law of the month.

Matlab Code:

%date of maternity filing [DATESTR,TIMESTR]=textread ('PregnantWomanFileTime.txt','%s%s'); F=cell2mat (datestr); F=f (:,1:7); F=tabulate (f); F=sortrows (F,1) Bar (Cell2mat (f (:,2)), 1);Set(GCA,'Xticklabel', F (:,1),'XTick', [1:length (F (:, 1))]);Title'time statistics for pregnant women to build files');%The following code is to rotate the horizontal axis labels Xtb=Get(GCA,'Xticklabel');%Get horizontal axis label handle XT=Get(GCA,'XTick');%get a horizontal axis scale handle YT=Get(GCA,'Ytick'); %get the vertical axis scale handle XTEXTP=xt;%the horizontal axis of each label placement position YTEXTP=yt (1) *ones (1, Length (XT)); text (XTEXTP,YTEXTP,XTB,'HorizontalAlignment',' Right','VerticalAlignment','Top','Rotation', $,'fontsize',Ten); Set(GCA,'Xticklabel',"');% to hide the original label

Final Build:

The Matlab code above will be analyzed below.

1. Read the datetime data in txt

If the TXT is a two-column value, such as "1981 1986", it is only necessary to use M = Load (' shuzhi.txt ') to read into the M-matrix, as shown in.

>> M = Load ('CoupleBirth.txt'); >> M (1:4=        1981        1986        1988        1993        1985        1989        1984        1984

and then you can use m (:, 1) and m (:, 2) Access these two columns of data.

But in this case we are not going to read the numeric data, but the string containing the date, we can no longer read with the  load function, to use the  textread function. Because it is two columns of data, you cannot use M=textread (' PregnantWomanFileTime.txt ', '%s '); To read, if hard to read like this, the date and time will be mixed in the returned cell array M, as follows:

 >> m=textread ( "  PregnantWomanFileTime.txt  , "  Span style= "color: #800000;" >%s   " );  >> M (1 : 4  ) ans  =  "   " "  10:12:52   " "  2014-11-21   " "  17:16:47   

To read two columns of data into a two-cell array, use:

 >> [Datestr,timestr]=textread ( "  , "  Span style= "color: #800000;" >%s%s   " );  >> datestr (1 : ) ans  =  " 2015-04-22   '   '  2014-11-21    '  2013-12-16    '  2013-12-26   

The Textread function returns an array of cells filled with data, both DATESTR and timestr are cellular arrays.

The cellular array is a special data type of MATLAB, which can be regarded as an all-encompassing universal matrix. By enclosing the parentheses (), the data in the cell array is accessed, and the corresponding cell is returned. Access the data in the cell array by enclosing the curly braces {}, and returning the contents of the corresponding cell.

>> [Datestr,timestr]=textread ('PregnantWomanFileTime.txt','%s%s');>> Datestr (1) ans='2015-04-22'>>class(Datestr (1)) Ans=Cell>> datestr{1}ans= --Geneva- A>>class(datestr{1}) Ans=Char

2, from 2015-04-22 to extract 2015-04

You can use regular expressions, but here we use the method of the Matrix, the method of the regular expression, which we introduce later. At present Datestr or cell array, we first turn the cell array into a char matrix, using the Cell2mat function.

>> [Datestr,timestr]=textread ('PregnantWomanFileTime.txt','%s%s');>>class(datestr) ans=Cell>> f=Cell2mat (DATESTR);>>class(f) ans=Char>> F (1:4,:) ans= --Geneva- A the- One- + -- A- - -- A- -

You can then extract the required characters for the Char matrix.

>> f=f (:,1:7); >> F (1:4=)-Geneva ---

3, the frequency of statistical month

If f is a one-dimensional numerical matrix, it is only necessary to use the Hist function, but because it is the frequency of the date character, the hist cannot be used.

>> hist (f)using ==> histinput arguments must be numeric.

Fortunately, Matlab provides another similar frequency statistic function, tabulate.

>> f=tabulate (f);>>FF='2015-04'[1386]    [6.4706]    '2014-11'[582]    [2.7171]    '2013-12'[ -]    [0.3922]    '2014-01'[766]    [3.5761]    '2014-09'[587]    [2.7404]    '2014-02'[616]    [2.8758]

Use the Sortrows function to sort F on the first column element.

>> F=sortrows (F,1);>>FF='2013-12'[ -]    [0.3922]    '2014-01'[766]    [3.5761]    '2014-02'[616]    [2.8758]    '2014-03'[ +]    [4.6685]    '2014-04'[977]    [4.5612]    '2014-05'[948]    [4.4258]    '2014-06'[961]    [4.4865]

4. Histogram mapping

The drawing is simple, and the bar function is fine. First we want F to turn into a one-dimensional matrix, because the above tabulate returns an array of cells. The row matrix and the matrix are all available. Then call bar to draw the data graph of the second column F, and use the first column F to set the X-axis label, plus the Title.

>> Bar (Cell2mat (:,2)),1); Set (GCA,'xticklabel', F (:,1),'xtick', [1 : Length (F (:,1))]); ' time statistics for pregnant women to build files ' ); % the code below is to rotate the horizontal axis labels

Ben came here to finish the drawing, but to see the x-axis labels crowded, completely unable to see, so the X-axis label display needs to be adjusted.

5. Rotate x-axis label

Matlab Date Frequency Statistics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.