Contenuto principale

Explore Basic Statistics on 2-D Plot

This example shows how to interactively explore and summarize plotted data using the Data Statistics tool. Using this tool, you can inspect and visualize basic statistics for a data series on the plot and save statistics to the MATLAB workspace for further analysis without writing code for each statistic.

In this example, you analyze sample data consisting of the mass (g) and volume (cubic cm) measurements of 15 rocks.

Mass = [250 320 180 405 289 340 210 370 295 310 282 400 195 355 275]';
Volume = [100 130 90 150 120 140 95 160 110 125 119 152 87 138 108]';
T = table(Mass,Volume);

Visualize Data

Visualize the mass and volume data for each rock using a scatter plot.

scatter(T,"Mass","Volume")

Figure contains an axes object. The axes object with xlabel Mass, ylabel Volume contains an object of type scatter.

Display Statistics on Plot

To use the Data Statistics tool, on the Tools tab of the figure, click Data Statistics. The Data Statistics tool displays descriptive statistics for the mass and volume measurements. To display specific statistics on the plot, select the check box for each statistic that you want to show.

For example, to display the mean mass and volume, select the mean check box in the X and Y columns. This action creates a vertical line and a horizontal line on the plot at the mean mass and volume and also automatically adds a legend.

The Data Statistics tool is appears to the right of a figure window that displays a scatter plot. In the Data Statistics tool, the X data mean and Y data mean statistics are selected In the figure, the X data mean and Y data mean are displayed as dashed vertical and horizontal lines, respectively.

If your plot contains multiple data series, in the Data Statistics for list, select the series for which you want to compute statistics. Selecting a different data series clears the statistics for the previously selected data series from the plot.

Save Statistics to Workspace

To save all of the statistics for further analysis, click Save to Workspace. The Data Statistics tool saves the statistics in scalar structures in the MATLAB workspace. You can specify a name for each structure.

In the Save Statistics to Workspace dialog box, the X and Y statistic structures are selected. Editable text boxes specify the name of the X statistic structure as massStats and the name of the Y statistic structure as volumeStats.

You can view the contents of each structure in the Variables editor.

openvar massStats
openvar volumeStats

The Variables editor displays the name, value, size, and class of each field in the structure massStats.

You can access the statistics in the structure by using dot notation. For example, access the standard deviation of the mass measurements and then compute the variance.

massVar = massStats.std .^ 2;

See Also

Functions

Topics