Main Content

linkdata

Automatically update charted data

Description

Use data linking to synchronize charts and their workspace variables. Linked charts automatically update with changes to workspace variables, and workspace variables automatically update with chart modifications made via the brush tool.

Link charts and workspace variables using thelinkdatafunction or by selectingfrom the figure toolbar.

Creation

Description

example

linkdata onturns on data linking mode for the current axes.

linkdata offturns off data linking mode.

linkdatatoggles the data linking mode between'on'and'off'.

example

linkdata showdialogopens theLinked Plot Data Sourcesdialog box. Use this syntax to interactively synchronize a chart with workspace variables.

linkdata(fig,___)specifies the data linking mode for the specified figure. Use single or double quotation marks around the options'on'and'off'. For example,linkdata(fig,'on').

l = linkdatacreates aLinkDataobject. This syntax is useful for querying the data linking mode.

l = linkdata(fig)creates aLinkDataobject for the specified figure.

Input Arguments

expand all

Target figure, specified as aFigureobject. If you do not specify the figure, then data linking is enabled or disabled for the current figure.

Properties

expand all

This property is read-only.

Data linking mode, specified as'off'or'on'.

Examples

collapse all

Create a bar chart and enable data linking. Data linking synchronizes the bar chart with thexandyvariables in the workspace.

x = linspace(-5,5,11); y = abs(x); bar(x,y) linkdataon

Bar chart. The bar at x = 0 has a height of 0.

Modify one of the values iny. Note that the bar chart immediately updates to reflect the change.

y(6) = 4;

Updated bar chart. The bar at x = 0 has a height of 4.

Interactively delete workspace values by selecting them in a chart, usinglinkdataand the brush tool.

Create some data to plot. Note thatxandycontain 21 values.

x = linspace(-5,5,21); y = -abs(x); lengthX = length(x) lengthY = length(y)
lengthX = 21 lengthY = 21

Plot the data, then enable both data linking and brushing.

scatter(x,y,'filled') linkdataonbrushon

Highlight the data values you want to delete. To remove the brushed data from the chart, right-click on a brushed value and selectRemovefrom the context menu.

Scatter plot with seven brushed data values

The chart is linked to its data sources, so deleting values from the chart also deletes them from the data sources. Note thatxandynow contain only 14 data values.

lengthX = length(x) lengthY = length(y)
lengthX = 14 lengthY = 14

Use data linking with complex numbers. When you plot complex data, the real and imaginary parts of the data do not correspond to different workspace variables. As a result, thelinkdatafunction cannot identify data sources for thex- andy-axes. To link complex data, you must manually specify the real part of the complex data as thex-component and the imaginary part as they-component.

First, create a matrix of complex data values and plot them. Then, open theLinked Plot Data Sourcesdialog box by callinglinkdata showdialog.

z = eig(randn(20,20)); plot(z) linkdatashowdialog

Line plot

Manually specify the real and imaginary parts of the data by typingreal(z)andimag(z)in the text boxes underXandY.

Linked plot data sources dialog box

Now the chart is linked to its data sources. If you changez, then the plot will update to reflect the new data.

To avoid using the dialog box, you can specify the data sources when first plotting the data values.

z = eig(randn(20,20)); plot(z,'XDataSource','real(z)','YDataSource','imag(z)') linkdataon

Tips

  • Iflinkdatacannot unambiguously identify data sources for a chart, then the chart will not synchronize with workspace variables. If you calllinkdataand your chart does not update when you change a variable, then open theLinked Plot Data Sourcesdialog box by callinglinkdata showdialogand manually link the chart to its data sources.

  • Thelinkdatafunction updates linked data sources and charts around twice a second. To smoothly animate changes in data values, create loops that execute two times per second or less. For more information, see thepausefunction.

  • Once a chart is synchronized with its workspace variables, programmatic changes to the data properties of the chart (for example,XData) do not update the workspace variables. Workspace variables only update when you modify plotted data interactively, using the brush tool.

Algorithms

数据连接连接图表和工作区riables using the chart's data source properties (for example,XDataSource). When you turn on data linking for a figure, MATLAB®compares variables in the workspace to plotted data in the figure. When it finds a match, MATLAB assigns that variable to the appropriate data source property of the chart.

Version History

Introduced in R2008a