MATLAB: Reading Data from Files

Reading in Data from a File

Example Book1.txt (Stored in C:\Users\…\Documents\Test1)

1<span style="color: #ff0000;">→</span>A<span style="color: #ff0000;">→</span>Small
2<span style="color: #ff0000;">→</span>B<span style="color: #ff0000;">→</span>Medium
3<span style="color: #ff0000;">→</span>C<span style="color: #ff0000;">→</span>Large
MyTable1=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book1.txt</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

This file uses the tab [→] delimiter (the tab key is used to represent that one moves to the next column). This can be specified.

MyTable1=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book1.txt</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">Delimiter</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">\t</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

Example Book2.csv (Stored in C:\Users\…\Documents\Test1)

1<span style="color: #ff0000;">,</span>A<span style="color: #ff0000;">,</span>Small
2<span style="color: #ff0000;">,</span>B<span style="color: #ff0000;">,</span>Medium
3<span style="color: #ff0000;">,</span>C<span style="color: #ff0000;">,</span>Large
MyTable1=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book2.csv</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

This file uses the comma [,] delimiter (the , is used to represent that one moves to the next column).This can be specified.

MyTable1=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book2.csv</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">Delimiter</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">,</span>'<span style="color: #0000ff;">)</span>

Example Book3.txt (Stored in C:\Users\…\Documents\Test1)

<span style="color: #ff0000;">Number→Letter→Size</span>
1→A→Small
2→B→Medium
3→C→Large
MyTable3=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book3.txt</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Delimiter</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">,</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">\t</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

The Table consists of a series of Column Vectors. Each Column is treated as a variable and has a VariableName. If the first line is of a different form to the rest, it will be used to assign the Variable Names. These are usually read in by default, if not you can add the additional arguments:

MyTable3=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book3.txt</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Delimiter</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">\t</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">ReadVariableNames</span><span style="color: #800080;">'</span>,1<span style="color: #0000ff;">)</span>

Alternatively if you don't want to read these in:

MyTable3=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book3.txt</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Delimiter</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">\t</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">ReadVariableNames</span><span style="color: #800080;">'</span>,0<span style="color: #0000ff;">)</span>

These can also be changed once the table is read in:

MyTable3<span style="color: #800000;">.</span><span style="color: #808000;">Properties</span><span style="color: #800000;">.</span><span style="color: #99ccff;">VariableNames</span>=<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">Num</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Let</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Size</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>

Example Book4.txt (Stored in C:\Users\…\Documents\Test1)

<span style="color: #ff0000;">This File was Created by Philip Yip on the 27/01/2019</span>
Number→Letter→Size
1→A→Small
2→B→Medium
3→C→Large

This Table has 1 HeaderLine it can be removed when the table is read in by using the additional arguments

MyTable4=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book4.txt</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Delimiter</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">\t</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">HeaderLine</span><span style="color: #800080;">'</span>,<span style="color: #ff0000;">1</span><span style="color: #0000ff;">)</span>

Example Book5.txt (Stored in C:\Users\…\Documents\Test1)

This File was Created by Philip Yip on the 27/01/2019
Number→Letter→Size
<span style="color: #ff0000;">%% 1 is the first number</span>
1→A→Small
<span style="color: #ff0000;">%% 2 is the second number</span>
2→B→Medium
<span style="color: #ff0000;">%% 3 is the third number</span>
3→C→Large

This Table has Comments denoted by %% they can be removed when the table is read in by using the additional arguments

MyTable5=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test1\Book5.txt</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Delimiter</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">\t</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">HeaderLine</span><span style="color: #800080;">'</span>,1,<span style="color: #800080;">'</span><span style="color: #ff0000;">CommentStyle</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">%%</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

A Column (Variable) may be selected from a table using Dot Notation.

MyTable5<span style="color: #333300;">.</span><span style="color: #99cc00;">Number</span>
MyTable5<span style="color: #333300;">.</span><span style="color: #99cc00;">Letter</span>
MyTable5<span style="color: #333300;">.</span><span style="color: #99cc00;">Size</span>

The last Column may be changed to categorical:

MyTable5<span style="color: #333300;">.</span><span style="color: #99cc00;">Size</span>=<span style="color: #0000ff;">categorical(</span>MyTable5<span style="color: #993300;">.</span><span style="color: #99cc00;">Size</span><span style="color: #0000ff;">)</span>
<span style="color: #0000ff;">categories(</span>MyTable5<span style="color: #993300;">.</span><span style="color: #99cc00;">Size</span><span style="color: #0000ff;">)</span>

The 2nd Column may be deleted using:

MyTable5<span style="color: #993300;">.</span><span style="color: #99cc00;">Letter</span>=<span style="color: #ff0000;">[]</span>

Reading in Data from Multiple Files

Suppose with have a folder C:\Users\…\Documents\Test2

And in this folder we have 3 files:

Book1.txt

This File was Created by Philip Yip on the 27/01/2019
Number→Letter→Size
%% 1 is the first number
1→A→Small
%% 2 is the second number
2→B→Medium
%% 3 is the third number
3→C→Large

Book2.txt

This File was Created by Philip Yip on the 27/01/2019
Number→Letter→Size
%% 4 is the fourth number
4→A→small
%% 5 is the fifth number
5→B→medium
%% 6 is the sixth number
6→C→large

Book3.txt

This File was Created by Philip Yip on the 27/01/2019
Number→Letter→Size
%% 7 is the seventh number
7→A→Small
%% 8 is the eight number
8→B→Medium
%% 9 is the ninth number
9→C→large

One can create a datastore to read multiple files within a folder.

MyDataStore=<span style="color: #0000ff;">datastore(</span><span style="color: #800080;">'</span><span style="color: #99cc00;">C:\Users\...\Documents\Test2</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

To preview the contents of the datastore (the first file) we can use:

<span style="color: #0000ff;">preview(</span>MyDataStore<span style="color: #0000ff;">)</span>

We have 1 HeaderLine in the preview.

<span style="color: #ff0000;">This File was Created by Philip Yip on the 27/01/2019</span>
Number→Letter→Size
%% 1 is the first number
1→A→Small
%% 2 is the second number
2→B→Medium
%% 3 is the third number
3→C→Large

We can specify this by using dot notation to change MyDataStore Properties:

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">NumHeaderLines</span>=1

To preview the contents of the MyDataStore (the first file) we can once again use:

<span style="color: #0000ff;">preview(</span>MyDataStore<span style="color: #0000ff;">)</span>

We have lines with comments

This File was Created by Philip Yip on the 27/01/2019
Number→Letter→Size
<span style="color: #ff0000;">%% 1 is the first number</span>
1→A→Small
<span style="color: #ff0000;">%% 2 is the second number</span>
2→B→Medium
<span style="color: #ff0000;">%% 3 is the third number</span>
3→C→Large

We can specify these once again by using dot notation to change MyDataStore Properties:

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">CommentStyle</span>=<span style="color: #800080;">'</span><span style="color: #ff0000;">%%</span><span style="color: #800080;">'</span>

We are using the tab [→] as the delimiter however if we wanted to change it to a , we could specify the delimiter using:

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">delimiter</span>=<span style="color: #800080;">'</span><span style="color: #ffcc00;">,</span><span style="color: #800080;">'</span>

To change the delimiter back to a tab [] we can use:

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">delimiter</span>=<span style="color: #800080;">'</span><span style="color: #ffcc00;">\t</span><span style="color: #800080;">'</span>

We can also view the formats of each variable (column) using

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">TextscanFormats</span>
 

%f is numeric

%q is non-numeric

%C is Categorical

Okay now we are happy with the settings of MyDataStore we can read the first file in MyDataStore which is Book1.txt and assign it to Data1 using:

Data1=<span style="color: #0000ff;">read(</span>MyDataStore<span style="color: #0000ff;">)</span>

We can read the second file in MyDataStore which is Book2.txt and assign it to Data2 using:

Data2=<span style="color: #0000ff;">read(</span>MyDataStore<span style="color: #0000ff;">)</span>

We can read the third file in MyDataStore which is Book3.txt and assign it to Data3 using:

Data3=<span style="color: #0000ff;">read(</span>MyDataStore<span style="color: #0000ff;">)</span>

Alternatively we can read the entire contents of MyDataStore and assign it to AllData using:

AllData=<span style="color: #0000ff;">readall(</span>MyDataStore<span style="color: #0000ff;">)</span>

We can once again use dot notation to look at each Variable (Column) of the Table AllData.

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">Number</span>
AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">Letter</span>
AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">Size</span>

Going back to MyDataStore if we wanted to change the third column so it is categorical we can use

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">TextscanFormats<span style="color: #666699;">{<span style="color: #ff0000;">3</span>}<span style="color: #000000;">=<span style="color: #800080;">'</span><span style="color: #ff0000;">%C</span><span style="color: #800080;">'</span></span></span></span>

If we also want to only select the first and third variables (columns) in MyDataStore we can use:

MyDataStore<span style="color: #993300;">.</span><span style="color: #99cc00;">SelectedVariableNames</span>
<span style="color: #99cc00;"><span style="color: #666699;"><span style="color: #000000;">=<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">Number</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Size</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span></span></span></span>
AllData2=<span style="color: #0000ff;">readall(</span>MyDataStore<span style="color: #0000ff;">)</span>

This time only the selected columns will be read and the third variable (column) will be categorical.

Going back to AllData we are interested in the variable (column) Size and as we didn't change the third column to be categorical before using readall we will create a categorical version of it as a new variable (column). To do this we can use:

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">Size </span>

Now we can use:

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">categorical(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #0000ff;">)</span>

Now we can view the categories using:

<span style="color: #0000ff;">categories(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #0000ff;">)</span>

Although we want only 3 categories we will find that we actually have 6 due to upper and lower case versions of each category being listed individually:

  • "Small" and "small"
  • "Medium" and "medium"
  • "Large" and "large"

To rectify this we can use:

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">mergecats(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">small</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Small</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">small</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">mergecats(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #666699;">{<span style="color: #800080;">'</span></span><span style="color: #ffcc00;">medium</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Medium</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;"><span style="color: #ffcc00;">medium</span></span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">mergecats(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;"><span style="color: #ffcc00;">large</span></span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Large</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">large</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

We can once again view the categories using:

<span style="color: #0000ff;">categories(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #0000ff;">)</span>

Now we have only 3 categories:

  • "small"
  • "medium"
  • "large"

Say we were indecisive and after changing all the categories to lower case we decided we instead we wanted the upper case values, we can also use:

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">renamecats(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">small</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">medium</span><span style="color: #800080;">','<span style="color: #ffcc00;">large</span>'</span><span style="color: #666699;">}</span>,<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">Small</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ffcc00;">Medium</span><span style="color: #800080;">','<span style="color: #ffcc00;">Large</span>'</span><span style="color: #666699;">}</span><span style="color: #0000ff;">)</span>

We can create a new table which sorts the data by the variable (column) SizeCats

AllDataSorted=<span style="color: #0000ff;">sortrows(</span>AllData,<span style="color: #800080;">'</span><span style="color: #ff0000;">SizeCats</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

This lists the column alphabetically:

  • "Large"
  • "Medium"
  • "Small"

If we want to instead list by the size from smallest to largest we can redefine the categories as ordinal

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">categorical(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>,<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">Small</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Medium</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">Large</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">ordinal</span><span style="color: #800080;">'</span>,<span style="color: #ff0000;">1</span><span style="color: #0000ff;">)</span>

Now we can create:

AllDataSorted2=<span style="color: #0000ff;">sortrows(</span>AllData,<span style="color: #800080;">'</span><span style="color: #ffcc00;">SizeCats</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

This lists the column ordinally:

  • "Small"
  • "Medium"
  • "Large"

We can remove the unnecessary column Size using:

AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">Size</span>=<span style="color: #ff0000;">[]</span>

We can then write this table to file to a text file processeddata.txt in the folder (C:\Users\…\Documents\Test2):

<span style="color: #0000ff;">writetable(</span>AllData,<span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test2\processeddata.txt</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

If we want an excel file instead we can use:

<span style="color: #0000ff;">writetable(</span>AllData,<span style="color: #800080;">'</span><span style="color: #ffcc00;">C:\Users\...\Documents\Test2\processeddata.xlsx</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

If we want to select all the categories that are not Small we can use:

NotSmallCatArray=<span style="color: #0000ff;">setdiff(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>,'<span style="color: #ffcc00;">Small</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

To convert to the categorical array to a cell array of characters we can use:

NotSmallCellArray=<span style="color: #0000ff;">cellstr(</span>NotSmallCatArray<span style="color: #0000ff;">)</span>
AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span>=<span style="color: #0000ff;">mergecats(</span>AllData<span style="color: #993300;">.</span><span style="color: #99cc00;">SizeCats</span><span style="color: #666699;"><span style="color: #800080;"><span style="color: #000000;">,NotSmallCellArray</span></span></span>,<span style="color: #800080;">'</span><span style="color: #ff9900;"><span style="color: #ffcc00;">not small</span></span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

Discretize

Supposing we have the following table of Student Test Results:

And we have the following criteria:

StudentScore<span style="color: #993300;">.</span><span style="color: #99cc00;">Marks</span>=<span style="color: #0000ff;">readtable(</span><span style="color: #800080;">'</span>...<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
Levels=[0;50;55;70;80;100]
Grades=<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ffcc00;">F</span><span style="color: #800080;">'</span>;<span style="color: #800080;">'</span><span style="color: #ffcc00;">D</span><span style="color: #800080;">'</span>;<span style="color: #800080;">'</span><span style="color: #ffcc00;">C</span><span style="color: #800080;">'</span>;<span style="color: #800080;">'</span><span style="color: #ffcc00;">B</span><span style="color: #800080;">'</span>;<span style="color: #800080;">'</span><span style="color: #ffcc00;">A</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>
StudentScore<span style="color: #800000;">.</span><span style="color: #99cc00;">Grades</span>=<span style="color: #0000ff;">discretize(</span>StudentScore<span style="color: #800000;">.</span><span style="color: #99cc00;">Marks</span>,Levels,<span style="color: #800080;">'</span><span style="color: #ffcc00;">categorical</span><span style="color: #800080;">'</span>,Grades<span style="color: #0000ff;">)</span>

Groups

a=[1;2;3;4;5;6;7;8;9;10]

b={'small';'small';'small';'medium';'medium';'medium';'medium';'large';'large';'large'}

c={'a';'a';'b';'b';'c';'c';'c';'c';'d';'d'}

MyTable=table(a,b,c)

[MyGroupsNums,MyGroupsValuesB,MyGroupsValuesC]=findgroups(MyTable.b,MyTable.c)

MyTable<span style="color: #993300;">.</span><span style="color: #99cc00;">b</span>=<span style="color: #0000ff;">categorical(</span>MyTable<span style="color: #993300;">.</span><span style="color: #99cc00;">b</span>,<span style="color: #666699;">{</span><span style="color: #800080;">'</span><span style="color: #ff9900;">small</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">medium</span><span style="color: #800080;">'</span>,<span style="color: #800080;">'</span><span style="color: #ff9900;">large</span><span style="color: #800080;">'</span><span style="color: #666699;">}</span>,<span style="color: #800080;">'</span><span style="color: #ff0000;">ordinal</span><span style="color: #800080;">'</span>,<span style="color: #ff0000;">1</span><span style="color: #0000ff;">)</span>

[MyGroupsNums,MyGroupsValuesB,MyGroupsValuesC]=findgroups(MyTable.b,MyTable.c)

 

 

A

The function is equal can be used to find if two variables a and b are equal:

c=isequal(a,b)

This function can be used to to determine if each value in the input vector d is a number or not. Each index of d which is not a number gives a logical value of 1 in the output vector e which has the same dimensions. If th index of d is not, not a number i.e. is a number then the corresponding index in e is given a logical value of 0:

e=isnan(d)

The function number of non-zero values can be used to determine the number of non-zero values in f and set this value to the output g:

g=nnz(f)

 

Merging Data

join

innerjoin

outerjoin

 

Split Apply

splitapply(@func,Data,Group)

mean max min mode

 

accumarray([Group1,Group2],Data,[],@func)

 

Merge Table

hurrs = readtable('hurricaneData1990s.txt');
mnth = month(hurrs.Timestamp,'name');
% Group the values in mnth.
[gNum1,gName1] = findgroups(mnth);
% TODO – Find the average wind speed (hurrs.Windspeed)
% by month
avgWS=splitapply(@mean,hurrs.Windspeed,gNum1)
bar(avgWS)
set(gca,'xticklabel',gName1)
gName1=monthNum2Name(gName1)
xticklabels(gName1)
xtickangle(45)