{"id":3079,"date":"2018-10-02T00:41:40","date_gmt":"2018-10-02T05:41:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3079"},"modified":"2018-10-04T08:56:21","modified_gmt":"2018-10-04T13:56:21","slug":"new-ways-to-arrange-and-plot-data-in-tables","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2018\/10\/02\/new-ways-to-arrange-and-plot-data-in-tables\/","title":{"rendered":"New Ways to Arrange and Plot Data in Tables"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Today I'd like to introduce a guest blogger, Stephen Doe, who works for the MATLAB Documentation team here at MathWorks. In today's post, Stephen shows us new functions for displaying, arranging, and plotting data in tables and timetables.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#a048cd49-5abc-4db7-b7bf-7f3ae706ac67\">Tables, Then and Now<\/a><\/li><li><a href=\"#1ef86267-a885-4a0c-8065-7252247544f2\">Read Table and Display First Few Rows<\/a><\/li><li><a href=\"#3d9cd707-8ca3-47f5-9f7d-4606c4ffd97b\">Move, Add, and Delete Table Variables<\/a><\/li><li><a href=\"#04474bc5-72d3-47db-b922-8b82e2b3a60e\">Convert to Timetable<\/a><\/li><li><a href=\"#e4f65ba6-64e5-484c-85d7-6d6239dbb33b\">Make Stacked Plot of Variables<\/a><\/li><li><a href=\"#124e17b3-b326-4b93-8995-164bebf5495c\">Convert Variables in Place<\/a><\/li><li><a href=\"#8fe4a01d-6e85-4a24-826a-abd362db7cf8\">Plots of Discrete Data<\/a><\/li><li><a href=\"#d8f6fcfb-3d29-43f7-98ba-4b7db431516f\">Other Functions to Rearrange or Join Tables<\/a><\/li><li><a href=\"#30c51223-1e7c-4c2e-9947-76486f64b285\">Tabled for Discussion<\/a><\/li><\/ul><\/div><h4>Tables, Then and Now<a name=\"a048cd49-5abc-4db7-b7bf-7f3ae706ac67\"><\/a><\/h4><p>In R2013b, MATLAB&reg; introduced the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/table.html\">table<\/a><\/tt> data type, as a convenient container for column-oriented data. And in R2016b, MATLAB introduced the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/timetable.html\">timetable<\/a><\/tt> data type, which is a table that has timestamped rows.<\/p><p>From the beginning, these data types offered advantages over cell arrays and structures. But over the course of several releases, the table and graphics development teams have added many new functions for tables and timetables. These functions add convenient ways to display and arrange tabular data. Also, they offer new ways to make plots or charts directly from tables, without the intermediate step of peeling out variables. As of R2018b, MATLAB boasts many new functions to help you make more effective use of tables and timetables.<\/p><h4>Read Table and Display First Few Rows<a name=\"1ef86267-a885-4a0c-8065-7252247544f2\"><\/a><\/h4><p>To begin, I will use the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/readtable.html\">readtable<\/a><\/tt> function to read data from a sample file that ships with MATLAB. The file <tt>outages.csv<\/tt> contains simulated data for electric power outages over a period of 12 years in the United States. The call to <tt>readtable<\/tt> returns a table, <tt>T<\/tt>, with six variables and 1468 rows, so I will suppress the output using a semicolon.<\/p><pre class=\"codeinput\">T = readtable(<span class=\"string\">'outages.csv'<\/span>);\r\n<\/pre><p>One typical way to examine the data in a large table is to display the first few rows of the table. You can use indexing to access a subset of rows (and\/or a subset of variables, for that matter). For example, this syntax returns the first three rows of <tt>T<\/tt>.<\/p><pre class=\"codeinput\">T(1:3,:)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;6 table\r\n      Region          OutageTime        Loss     Customers     RestorationTime         Cause     \r\n    ___________    ________________    ______    __________    ________________    ______________\r\n    'SouthWest'    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    'winter storm'\r\n    'SouthEast'    2003-01-23 00:49    530.14    2.1204e+05                 NaT    'winter storm'\r\n    'SouthEast'    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    'winter storm'\r\n<\/pre><p>I have a confession to make: I have written many table examples, using that syntax. And occasionally, I <b>still<\/b> catch myself starting with code like <tt>T(3,:)<\/tt>, which accesses only one row.<\/p><p>Happily, in R2016b we added the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/head.html\">head<\/a><\/tt> function to return the top rows of a table. Here's the call to return the first three rows using the <tt>head<\/tt> function.<\/p><pre class=\"codeinput\">head(T,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;6 table\r\n      Region          OutageTime        Loss     Customers     RestorationTime         Cause     \r\n    ___________    ________________    ______    __________    ________________    ______________\r\n    'SouthWest'    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    'winter storm'\r\n    'SouthEast'    2003-01-23 00:49    530.14    2.1204e+05                 NaT    'winter storm'\r\n    'SouthEast'    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    'winter storm'\r\n<\/pre><p>Similarly, <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/tail.html\">tail<\/a><\/tt> returns the bottom rows of a table. (If you do not specify the number of rows, then <tt>head<\/tt> and <tt>tail<\/tt> return eight rows.)<\/p><h4>Move, Add, and Delete Table Variables<a name=\"3d9cd707-8ca3-47f5-9f7d-4606c4ffd97b\"><\/a><\/h4><p>After examining your table, you might find that you want to organize your table by moving related variables next to each other. For example, in <tt>T<\/tt> you might want to move <tt>Region<\/tt> and <tt>Cause<\/tt> so that they are together.<\/p><p>One way to move table variables is by indexing. But if you use indexing and want to keep all the variables, then you must specify them all in order, as shown in this syntax.<\/p><pre class=\"language-matlab\">T = T(:,{<span class=\"string\">'OutageTime'<\/span>,<span class=\"string\">'Loss'<\/span>,<span class=\"string\">'Customers'<\/span>,<span class=\"string\">'RestorationTime'<\/span>,<span class=\"string\">'Region'<\/span>,<span class=\"string\">'Cause'<\/span>})\r\n<\/pre><p>You also can use numeric indices. While more compact, this syntax is less readable.<\/p><pre class=\"language-matlab\">T = T(:,[2:5 1 6])\r\n<\/pre><p>When your table has many variables, it is awkward to move variables using indexing. Starting in R2018a, you can use the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/movevars.html\">movevars<\/a><\/tt> function instead. Using <tt>movevars<\/tt>, you only have to specify the variables of interest. Move the <tt>Region<\/tt> variable so it is before <tt>Cause<\/tt>.<\/p><pre class=\"codeinput\">T = movevars(T,<span class=\"string\">'Region'<\/span>,<span class=\"string\">'Before'<\/span>,<span class=\"string\">'Cause'<\/span>);\r\nhead(T,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;6 table\r\n       OutageTime        Loss     Customers     RestorationTime       Region           Cause     \r\n    ________________    ______    __________    ________________    ___________    ______________\r\n    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    'SouthWest'    'winter storm'\r\n    2003-01-23 00:49    530.14    2.1204e+05                 NaT    'SouthEast'    'winter storm'\r\n    2003-02-07 21:15     289.4    1.4294e+05    2003-02-17 08:14    'SouthEast'    'winter storm'\r\n<\/pre><p>It is also likely that you want to add data to your table. For example, let's calculate the duration of the power outages in <tt>T<\/tt>. Specify the format to display the duration in days.<\/p><pre class=\"codeinput\">OutageDuration = T.RestorationTime - T.OutageTime;\r\nOutageDuration.Format = <span class=\"string\">'dd:hh:mm:ss'<\/span>;\r\n<\/pre><p>It is easy to add <tt>OutageDuration<\/tt> to the end of a table using dot notation.<\/p><pre class=\"language-matlab\">T.OutageDuration = OutageDuration;\r\n<\/pre><p>However, you might want to add it at another location in <tt>T<\/tt>. In R2018a, you can use the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/addvars.html\">addvars<\/a><\/tt> function. Add <tt>OutageDuration<\/tt> so that it is after <tt>OutageTime<\/tt>.<\/p><pre class=\"codeinput\">T = addvars(T,OutageDuration,<span class=\"string\">'After'<\/span>,<span class=\"string\">'OutageTime'<\/span>);\r\nhead(T,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;7 table\r\n       OutageTime       OutageDuration     Loss     Customers     RestorationTime       Region           Cause     \r\n    ________________    ______________    ______    __________    ________________    ___________    ______________\r\n    2002-02-01 12:18     06:04:32:00      458.98    1.8202e+06    2002-02-07 16:50    'SouthWest'    'winter storm'\r\n    2003-01-23 00:49             NaN      530.14    2.1204e+05                 NaT    'SouthEast'    'winter storm'\r\n    2003-02-07 21:15     09:10:59:00       289.4    1.4294e+05    2003-02-17 08:14    'SouthEast'    'winter storm'\r\n<\/pre><p>Now, let's remove <tt>RestorationTime<\/tt>. You can easily remove variables using dot notation and an empty array.<\/p><pre class=\"language-matlab\">T.RestorationTime = [];\r\n<\/pre><p>However, in R2018a there is also a function to remove table variables. To remove <tt>RestorationTime<\/tt>, use the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/removevars.html\">removevars<\/a><\/tt> function.<\/p><pre class=\"codeinput\">T = removevars(T,<span class=\"string\">'RestorationTime'<\/span>);\r\nhead(T,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;6 table\r\n       OutageTime       OutageDuration     Loss     Customers       Region           Cause     \r\n    ________________    ______________    ______    __________    ___________    ______________\r\n    2002-02-01 12:18     06:04:32:00      458.98    1.8202e+06    'SouthWest'    'winter storm'\r\n    2003-01-23 00:49             NaN      530.14    2.1204e+05    'SouthEast'    'winter storm'\r\n    2003-02-07 21:15     09:10:59:00       289.4    1.4294e+05    'SouthEast'    'winter storm'\r\n<\/pre><h4>Convert to Timetable<a name=\"04474bc5-72d3-47db-b922-8b82e2b3a60e\"><\/a><\/h4><p>If your table contains dates and times in a <tt>datetime<\/tt> array, you can easily convert it to a timetable using the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/table2timetable.html\">table2timetable<\/a><\/tt> function. In this example, <tt>table2timetable<\/tt> converts the values in <tt>OutageTime<\/tt> to <i>row times<\/i>. Row times are time stamps that label the rows of a timetable.<\/p><pre class=\"codeinput\">TT = table2timetable(T);\r\nhead(TT,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;5 timetable\r\n       OutageTime       OutageDuration     Loss     Customers       Region           Cause     \r\n    ________________    ______________    ______    __________    ___________    ______________\r\n    2002-02-01 12:18     06:04:32:00      458.98    1.8202e+06    'SouthWest'    'winter storm'\r\n    2003-01-23 00:49             NaN      530.14    2.1204e+05    'SouthEast'    'winter storm'\r\n    2003-02-07 21:15     09:10:59:00       289.4    1.4294e+05    'SouthEast'    'winter storm'\r\n<\/pre><p>When you display a timetable, it looks very similar to a table. One important difference is that a timetable has fewer variables than you might expect by glancing at the display. <tt>TT<\/tt> has five variables, not six. The vector of row times, <tt>OutageTime<\/tt>, is not considered a timetable variable, since its values label the rows. However, you can still access the row times using dot notation, as in <tt>T.OutageTime<\/tt>. You can use the vector of row times as an input argument to a function. For example, you can use it as the <i>x<\/i>-axis of a plot.<\/p><p>The row times of a timetable do not have to be ordered. If you want to be sure that the rows of a timetable are sorted by the row times, use the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/sortrows.html\">sortrows<\/a><\/tt> function.<\/p><pre class=\"codeinput\">TT = sortrows(TT);\r\nhead(TT,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;5 timetable\r\n       OutageTime       OutageDuration     Loss     Customers       Region           Cause     \r\n    ________________    ______________    ______    __________    ___________    ______________\r\n    2002-02-01 12:18     06:04:32:00      458.98    1.8202e+06    'SouthWest'    'winter storm'\r\n    2002-03-05 17:53     04:20:48:00      96.563    2.8666e+05    'MidWest'      'wind'        \r\n    2002-03-16 06:18     02:17:05:00      186.44    2.1275e+05    'MidWest'      'severe storm'\r\n<\/pre><h4>Make Stacked Plot of Variables<a name=\"e4f65ba6-64e5-484c-85d7-6d6239dbb33b\"><\/a><\/h4><p>Now I will show you why I converted <tt>T<\/tt> to a timetable. Starting in R2018b, you can plot the variables of a table or timetable in a <i>stacked plot<\/i>. In a stacked plot, the variables are plotted in separate <i>y<\/i>-axes, but using a common <i>x<\/i>-axis. And if you make a stacked plot from a timetable, the <i>x<\/i>-values are the row times.<\/p><p>To plot the variables of <tt>TT<\/tt>, use the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/stackedplot.html\">stackedplot<\/a><\/tt> function. The function plots variables that can be plotted (such as numeric, <tt>datetime<\/tt>, and <tt>categorical<\/tt> arrays) and ignores variables that cannot be plotted. <tt>stackedplot<\/tt> also returns properties of the stacked plot as an object that allows customization of the stacked plot.<\/p><pre class=\"codeinput\">s = stackedplot(TT)\r\n<\/pre><pre class=\"codeoutput\">s = \r\n  StackedLineChart with properties:\r\n\r\n         SourceTable: [1468&times;5 timetable]\r\n    DisplayVariables: {'OutageDuration'  'Loss'  'Customers'}\r\n               Color: [0 0.4470 0.7410]\r\n           LineStyle: '-'\r\n           LineWidth: 0.5000\r\n              Marker: 'none'\r\n          MarkerSize: 6\r\n\r\n  Use GET to show all properties\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2018\/NewWaysToArrangeAndPlotTablesLS_SD_FINAL_01.png\" alt=\"\"> <p>One thing you can tell right away from this plot is that there must be a few timetable rows with bad data. There is one point for a power outage that supposedly lasted for over 9,000 days (or 24 years), which would mean it ended some time in the 2040s.<\/p><h4>Convert Variables in Place<a name=\"124e17b3-b326-4b93-8995-164bebf5495c\"><\/a><\/h4><p>The <tt>stackedplot<\/tt> function ignored the <tt>Region<\/tt> and <tt>Cause<\/tt> variables, because these variables are cell arrays of character vectors. You might want to convert these variables to a different, and more useful, data type. While you can convert variables one at a time, there is now a more convenient way to convert all table variables of a specified data type.<\/p><p>Starting in R2018b, you can convert table variables in place using the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/convertvars.html\">convertvars<\/a><\/tt> function. For example, identify all the cell arrays of character vectors in <tt>TT<\/tt> (using <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/iscellstr.html\">iscellstr<\/a><\/tt>) and convert them to <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/categorical.html\">categorical<\/a><\/tt> arrays. Now <tt>Region<\/tt> and <tt>Cause<\/tt> contain discrete values assigned to categories. Categorical values are displayed without any quotation marks.<\/p><pre class=\"codeinput\">TT = convertvars(TT,@iscellstr,<span class=\"string\">'categorical'<\/span>);\r\nhead(TT,3)\r\n<\/pre><pre class=\"codeoutput\">ans =\r\n  3&times;5 timetable\r\n       OutageTime       OutageDuration     Loss     Customers      Region         Cause    \r\n    ________________    ______________    ______    __________    _________    ____________\r\n    2002-02-01 12:18     06:04:32:00      458.98    1.8202e+06    SouthWest    winter storm\r\n    2002-03-05 17:53     04:20:48:00      96.563    2.8666e+05    MidWest      wind        \r\n    2002-03-16 06:18     02:17:05:00      186.44    2.1275e+05    MidWest      severe storm\r\n<\/pre><h4>Plots of Discrete Data<a name=\"8fe4a01d-6e85-4a24-826a-abd362db7cf8\"><\/a><\/h4><p>If your table or timetable has variables with values that belong to a finite set of discrete categories, then there are other interesting plots that you can make. Starting in R2017a, you can make a <i>heat map<\/i> of any two variables that contain discrete values using the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmap.html\">heatmap<\/a><\/tt> function. For example, make a heat map of the <tt>Region<\/tt> and <tt>Cause<\/tt> variables to visualize where and why outages occur. Again, <tt>heatmap<\/tt> returns an object so you can customize the plot.<\/p><pre class=\"codeinput\">h = heatmap(TT,<span class=\"string\">'Region'<\/span>,<span class=\"string\">'Cause'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">h = \r\n  HeatmapChart (Count of Cause vs. Region) with properties:\r\n\r\n      SourceTable: [1468&times;5 timetable]\r\n        XVariable: 'Region'\r\n        YVariable: 'Cause'\r\n    ColorVariable: ''\r\n      ColorMethod: 'count'\r\n\r\n  Use GET to show all properties\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2018\/NewWaysToArrangeAndPlotTablesLS_SD_FINAL_02.png\" alt=\"\"> <p>You also can make a pie chart of any <tt>categorical<\/tt> variable (as of R2014b), using the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/pie.html\">pie<\/a><\/tt> function. However, you cannot call <tt>pie<\/tt> on a table. So, to make a pie chart of the power outages by region, use dot notation to access the <tt>Region<\/tt> variable.<\/p><pre class=\"codeinput\">pie(TT.Region)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2018\/NewWaysToArrangeAndPlotTablesLS_SD_FINAL_03.png\" alt=\"\"> <h4>Other Functions to Rearrange or Join Tables<a name=\"d8f6fcfb-3d29-43f7-98ba-4b7db431516f\"><\/a><\/h4><p>MATLAB also has other functions to reorganize variables in more complex ways, and to join tables. I won't show them all in action, but I will describe some of them briefly. All these functions work with both tables and timetables.<\/p><p>R2018a includes functions to:<\/p><div><ul><li>Split and merge multicolumn variables (<tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/splitvars.html\">splitvars<\/a><\/tt>, <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mergevars.html\">mergevars<\/a><\/tt>)<\/li><\/ul><\/div><div><ul><li>Reorient rows to become variables (<tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rows2vars.html\">rows2vars<\/a><\/tt>)<\/li><\/ul><\/div><div><ul><li>Invert a nested table-in-table (<tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/inner2outer.html\">inner2outer<\/a><\/tt>)<\/li><\/ul><\/div><p>And from the original release of tables in R2013b, there are functions to:<\/p><div><ul><li>Stack and unstack tables (<tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/stack.html\">stack<\/a><\/tt>, <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/unstack.html\">unstack<\/a><\/tt>)<\/li><\/ul><\/div><div><ul><li>Join tables, including inner and outer joins (<tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/table.join.html\">join<\/a><\/tt>, <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/innerjoin.html\">innerjoin<\/a><\/tt>, <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/outerjoin.html\">outerjoin<\/a><\/tt>)<\/li><\/ul><\/div><h4>Tabled for Discussion<a name=\"30c51223-1e7c-4c2e-9947-76486f64b285\"><\/a><\/h4><p>Let's table discussion of these new functions for now. But we are eager to hear about your reactions to them. Do they help you make more effective use of tables and timetables? Please let us know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3079#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d24e865398194695aee62f4577fb3075() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='d24e865398194695aee62f4577fb3075 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d24e865398194695aee62f4577fb3075';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2018 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_d24e865398194695aee62f4577fb3075()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><\/div><!--\r\nd24e865398194695aee62f4577fb3075 ##### SOURCE BEGIN #####\r\n%% New Ways to Arrange and Plot Data in Tables\r\n% Today I'd like to introduce a guest blogger, Stephen Doe, who works for\r\n% the MATLAB Documentation team here at MathWorks. In today's post, Stephen\r\n% shows us new functions for displaying, arranging, and plotting data in\r\n% tables and timetables.\r\n%\r\n%% Tables, Then and Now\r\n% In R2013b, MATLAB(R) introduced the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/table.html table>| data type,\r\n% as a convenient container for column-oriented data. And in R2016b, MATLAB\r\n% introduced the |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/timetable.html\r\n% timetable>| data type, which is a table that has timestamped rows.\r\n%\r\n% From the beginning, these data types offered advantages over cell arrays\r\n% and structures. But over the course of several releases, the table and\r\n% graphics development teams have added many new functions for tables and\r\n% timetables. These functions add convenient ways to display and arrange\r\n% tabular data. Also, they offer new ways to make plots or charts directly\r\n% from tables, without the intermediate step of peeling out variables. As\r\n% of R2018b, MATLAB boasts many new functions to help you make more\r\n% effective use of tables and timetables.\r\n\r\n%% Read Table and Display First Few Rows\r\n% To begin, I will use the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/readtable.html readtable>|\r\n% function to read data from a sample file that ships with MATLAB. The file\r\n% |outages.csv| contains simulated data for electric power outages over a\r\n% period of 12 years in the United States. The call to |readtable| returns\r\n% a table, |T|, with six variables and 1468 rows, so I will suppress the\r\n% output using a semicolon.\r\nT = readtable('outages.csv');\r\n\r\n%%\r\n% One typical way to examine the data in a large table is to display the\r\n% first few rows of the table. You can use indexing to access a subset of\r\n% rows (and\/or a subset of variables, for that matter). For example, this\r\n% syntax returns the first three rows of |T|.\r\n\r\nT(1:3,:)\r\n\r\n%%\r\n% I have a confession to make: I have written many table examples, using\r\n% that syntax. And occasionally, I *still* catch myself starting with code\r\n% like |T(3,:)|, which accesses only one row.\r\n%\r\n% Happily, in R2016b we added the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/head.html head>| function to\r\n% return the top rows of a table. Here's the call to return the first three\r\n% rows using the |head| function.\r\n%\r\nhead(T,3)\r\n%%\r\n% Similarly, |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/tail.html tail>|\r\n% returns the bottom rows of a table. (If you do not specify the number of\r\n% rows, then |head| and |tail| return eight rows.)\r\n\r\n%% Move, Add, and Delete Table Variables\r\n% After examining your table, you might find that you want to organize your\r\n% table by moving related variables next to each other. For example, in |T|\r\n% you might want to move |Region| and |Cause| so that they are together.\r\n%\r\n% One way to move table variables is by indexing. But if you use indexing\r\n% and want to keep all the variables, then you must specify them all in\r\n% order, as shown in this syntax.\r\n%\r\n%   T = T(:,{'OutageTime','Loss','Customers','RestorationTime','Region','Cause'})\r\n%\r\n% You also can use numeric indices. While more compact, this syntax is less\r\n% readable.\r\n%\r\n%   T = T(:,[2:5 1 6])\r\n%\r\n% When your table has many variables, it is awkward to move\r\n% variables using indexing. Starting in R2018a, you can use the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/movevars.html movevars>| function\r\n% instead. Using |movevars|, you only have to specify the variables of\r\n% interest. Move the |Region| variable so it is before |Cause|.\r\nT = movevars(T,'Region','Before','Cause');\r\nhead(T,3)\r\n\r\n%%\r\n% It is also likely that you want to add data to your table. For example,\r\n% let's calculate the duration of the power outages in |T|. Specify the\r\n% format to display the duration in days.\r\nOutageDuration = T.RestorationTime - T.OutageTime;\r\nOutageDuration.Format = 'dd:hh:mm:ss';\r\n\r\n%%\r\n% It is easy to add |OutageDuration| to the end of a table using dot\r\n% notation.\r\n%\r\n%   T.OutageDuration = OutageDuration;\r\n%\r\n% However, you might want to add it at another location in |T|. In R2018a,\r\n% you can use the |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/addvars.html\r\n% addvars>| function. Add |OutageDuration| so that it is after\r\n% |OutageTime|.\r\nT = addvars(T,OutageDuration,'After','OutageTime');\r\nhead(T,3)\r\n\r\n%%\r\n% Now, let's remove |RestorationTime|. You can easily remove variables\r\n% using dot notation and an empty array.\r\n%\r\n%   T.RestorationTime = [];\r\n%\r\n% However, in R2018a there is also a function to remove table variables. To\r\n% remove |RestorationTime|, use the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/removevars.html removevars>|\r\n% function.\r\nT = removevars(T,'RestorationTime');\r\nhead(T,3)\r\n\r\n%% Convert to Timetable\r\n% If your table contains dates and times in a |datetime| array, you can\r\n% easily convert it to a timetable using the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/table2timetable.html\r\n% table2timetable>| function. In this example, |table2timetable| converts\r\n% the values in |OutageTime| to _row times_. Row times are time stamps that\r\n% label the rows of a timetable.\r\nTT = table2timetable(T);\r\nhead(TT,3)\r\n\r\n%%\r\n% When you display a timetable, it looks very similar to a table. One\r\n% important difference is that a timetable has fewer variables than you\r\n% might expect by glancing at the display. |TT| has five variables, not\r\n% six. The vector of row times, |OutageTime|, is not considered a timetable\r\n% variable, since its values label the rows. However, you can still access\r\n% the row times using dot notation, as in |T.OutageTime|. You can use the\r\n% vector of row times as an input argument to a function. For example, you\r\n% can use it as the _x_-axis of a plot.\r\n%\r\n% The row times of a timetable do not have to be ordered. If you want to\r\n% be sure that the rows of a timetable are sorted by the row times, use the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/sortrows.html sortrows>|\r\n% function.\r\nTT = sortrows(TT);\r\nhead(TT,3)\r\n\r\n%% Make Stacked Plot of Variables\r\n% Now I will show you why I converted |T| to a timetable. Starting in\r\n% R2018b, you can plot the variables of a table or timetable in a _stacked\r\n% plot_. In a stacked plot, the variables are plotted in separate _y_-axes,\r\n% but using a common _x_-axis. And if you make a stacked plot from a\r\n% timetable, the _x_-values are the row times.\r\n%\r\n% To plot the variables of |TT|, use the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/stackedplot.html\r\n% stackedplot>| function. The function plots variables that can be plotted\r\n% (such as numeric, |datetime|, and |categorical| arrays) and ignores\r\n% variables that cannot be plotted. |stackedplot| also returns properties\r\n% of the stacked plot as an object that allows customization of the stacked\r\n% plot.\r\ns = stackedplot(TT)\r\n\r\n%%\r\n% One thing you can tell right away from this plot is that there must be a\r\n% few timetable rows with bad data. There is one point for a power outage\r\n% that supposedly lasted for over 9,000 days (or 24 years), which would\r\n% mean it ended some time in the 2040s.\r\n\r\n%% Convert Variables in Place\r\n% The |stackedplot| function ignored the |Region| and |Cause| variables,\r\n% because these variables are cell arrays of character vectors. You might\r\n% want to convert these variables to a different, and more useful, data\r\n% type. While you can convert variables one at a time, there is now a more\r\n% convenient way to convert all table variables of a specified data type.\r\n%\r\n% Starting in R2018b, you can convert table variables in place using the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/convertvars.html\r\n% convertvars>| function. For example, identify all the cell arrays of\r\n% character vectors in |TT| (using\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/iscellstr.html iscellstr>|)\r\n% and convert them to\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/categorical.html\r\n% categorical>| arrays. Now |Region| and |Cause| contain discrete values\r\n% assigned to categories. Categorical values are displayed without any\r\n% quotation marks.\r\nTT = convertvars(TT,@iscellstr,'categorical');\r\nhead(TT,3)\r\n\r\n%% Plots of Discrete Data\r\n% If your table or timetable has variables with values that belong to a\r\n% finite set of discrete categories, then there are other interesting plots\r\n% that you can make. Starting in R2017a, you can make a _heat map_ of any\r\n% two variables that contain discrete values using the\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmap.html heatmap>|\r\n% function. For example, make a heat map of the |Region| and |Cause|\r\n% variables to visualize where and why outages occur. Again, |heatmap|\r\n% returns an object so you can customize the plot.\r\nh = heatmap(TT,'Region','Cause')\r\n\r\n%%\r\n% You also can make a pie chart of any |categorical| variable (as of\r\n% R2014b), using the |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/pie.html\r\n% pie>| function. However, you cannot call |pie| on a table. So, to make a\r\n% pie chart of the power outages by region, use dot notation to access the\r\n% |Region| variable.\r\npie(TT.Region)\r\n\r\n%% Other Functions to Rearrange or Join Tables\r\n% MATLAB also has other functions to reorganize variables in more complex\r\n% ways, and to join tables. I won't show them all in action, but I will\r\n% describe some of them briefly. All these functions work with both tables\r\n% and timetables.\r\n%\r\n% R2018a includes functions to:\r\n%\r\n% * Split and merge multicolumn variables\r\n% (|<https:\/\/www.mathworks.com\/help\/matlab\/ref\/splitvars.html splitvars>|,\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/mergevars.html mergevars>|)\r\n%\r\n% * Reorient rows to become variables\r\n% (|<https:\/\/www.mathworks.com\/help\/matlab\/ref\/rows2vars.html rows2vars>|)\r\n%\r\n% * Invert a nested table-in-table\r\n% (|<https:\/\/www.mathworks.com\/help\/matlab\/ref\/inner2outer.html\r\n% inner2outer>|)\r\n%\r\n% And from the original release of tables in R2013b, there are functions\r\n% to:\r\n%\r\n% * Stack and unstack tables\r\n% (|<https:\/\/www.mathworks.com\/help\/matlab\/ref\/stack.html stack>|,\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/unstack.html unstack>|)\r\n%\r\n% * Join tables, including inner and outer joins\r\n% (|<https:\/\/www.mathworks.com\/help\/matlab\/ref\/table.join.html join>|,\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/innerjoin.html innerjoin>|,\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/outerjoin.html outerjoin>|)\r\n\r\n%% Tabled for Discussion\r\n% Let's table discussion of these new functions for now. But we are eager\r\n% to hear about your reactions to them. Do they help you make more\r\n% effective use of tables and timetables? Please let us know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=3079#respond here>.\r\n##### SOURCE END ##### d24e865398194695aee62f4577fb3075\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2018\/NewWaysToArrangeAndPlotTablesLS_SD_FINAL_03.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Today I'd like to introduce a guest blogger, Stephen Doe, who works for the MATLAB Documentation team here at MathWorks. In today's post, Stephen shows us new functions for displaying, arranging, and plotting data in tables and timetables.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2018\/10\/02\/new-ways-to-arrange-and-plot-data-in-tables\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[57,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3079"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3079"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3079\/revisions"}],"predecessor-version":[{"id":3105,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3079\/revisions\/3105"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}