Main Content

Clean Messy Data and Locate Extrema Using Live Editor Tasks

You can interactively preprocess data using sequences of Live Editor tasks, visualizing the data at each step. This example uses five tasks to clean noisy data with missing values and outliers in order to identify local minima and maxima. For more information on Live Editor tasks, see Add Interactive Tasks to a Live Script.

First, create and plot a vector of messy data, which contains four NaN values and five outliers.

x = 1:100;
data = cos(2*pi*0.05*x+2*pi*rand) + 0.5*randn(1,100);
data(20:20:80) = NaN;
data(10:20:90) = [-50 40 30 -45 35];

To plot the messy data, open the Create Plot task. Start by typing the keyword plot in a code block, and then click Create Plot when it appears in the menu. Select the plot type and input data to plot the data.

To see the code that this task generates, expand the task display by clicking Small arrow pointing down at the bottom of the task parameter area.

Live Task

Figure contains an axes object. The axes object with title x vs. data, xlabel x, ylabel data contains an object of type line. This object represents data.

Fill Missing Data

To replace NaN values in the data and visualize the results, open the Clean Missing Data task. Start by typing the keyword missing in a code block, and then click Clean Missing Data when it appears in the menu. Select the input data and the cleaning method to plot the filled data automatically.

To see the code that this task generates, expand the task display by clicking Small arrow pointing down at the bottom of the task parameter area.

Live Task

Figure contains an axes object. The axes object with title Number of filled missing entries: 4 contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Cleaned data, Filled missing entries.

Fill Outliers

You can now remove the outliers from the cleaned data in the previous task by using the Clean Outlier Data task. Type the keyword outliers in a new code block and click Clean Outlier Data to open the task. Select cleanedData as the input data. You can customize the methods for cleaning and detecting outliers and adjust the threshold to find more or fewer outliers.

To see the code that this task generates, expand the task display by clicking Small arrow pointing down at the bottom of the task parameter area.

Live Task

Figure contains an axes object. The axes object with title Number of outliers cleaned: 5 contains 5 objects of type line. One or more of the lines displays its values using only markers These objects represent Input data, Cleaned data, Outliers, Filled outliers, Outlier thresholds.

Smooth Data

Next, smooth the cleaned data from the previous task by using the Smooth Data task. Type the keyword smooth and click the task when it appears. Select cleanedData2, the output from the previous task, as the input data. Select a smoothing method, and adjust the smoothing factor for more or less smoothing.

To see the code that this task generates, expand the task display by clicking Small arrow pointing down at the bottom of the task parameter area.

Live Task

Figure contains an axes object. The axes object contains 2 objects of type line. These objects represent Input data, Smoothed data.

Locate Extrema

Finally, start typing the keyword extrema and click Find Local Extrema. Use smoothedData as the input data and change the extrema type to find both the local maxima and local minima of the cleaned, smoothed data. You can adjust the local extrema parameters to find more or fewer maxima and minima.

To see the code that this task generates, expand the task display by clicking Small arrow pointing down at the bottom of the task parameter area.

Live Task

Figure contains an axes object. The axes object with title Number of extrema: 11 contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Input data, Local maxima, Local minima.

See Also

Live Editor Tasks

Functions

Related Topics