Automatically update graphs when variables change
linkobj = linkdata(figure_handle)
linkdata on turns on data linking for the
linkdata off turns data linking off.
linkdata by itself toggles the
state of data linking.
linkdata(figure_handle,...) applies the
function to the specified figure handle.
linkobj = linkdata(figure_handle) returns
a linkdata object for the specified figure.
The object has one read-only property,
which is set to either
depending on the linked state of the figure.
Data linking connects graphs in figure windows to variables
in the base or a function's workspace via their
ZDataSource properties. When you turn on
data linking for a figure, MATLAB® compares variables in the current
(base or function caller) workspace with the
ZData properties of graphs in the affected
figure to try to match them. When a match is found, the appropriate
the graph are set to the matching variables.
Any subsequent changes to linked variables are reflected in graphs that use them as data sources and in the Variables editor, if the linked variables are displayed there. Conversely, any changes to plotted data values made at the command line, in the Variables editor, or with the Brush tool (such as deleting or replacing data points), are immediately reflected in the workspace variables linked to the data points.
When a figure containing graphs is linked and any variable identified
ZDataSource changes its values in the
workspace, all graphs displaying it in that and other linked figures
automatically update. This operation is equivalent to automatically
refreshdata function on the corresponding
figure when a variable changes.
Linked figure windows identify themselves by the appearance
of the Linked Plot information bar at the top of the window. When
a figure, the Linked Plot information bar is removed. If
unambiguously identify data sources for a graph in a linked figure,
it reports this via the Linked Plot information bar, which gives the
user an opportunity to identify data sources. The information bar
displays a warning icon and a message, No graphics have
data sources and also prompts fix it.
Clicking fix it opens the Specify Data Source
Properties dialog box for identifying variable names and ranges of
data sources used in the graph.
Create two variables, plot them as area charts, and link the plot to them:
x = 1:20; y = rand(20,3); area(x,y) linkdata on
Change values for linked
y in the workspace:
y(10,:) = 0;
Delete a figure if it is not linked, based on a returned
fig = figure; ld = linkdata(fig)
ld = graphics.linkdata
if strcmp(ld.Enable,'off') delete(fig) end
If a plotting function can display a complex variable, then you can link such plots. To do so, you need to describe the data sources as expressions to separate the real and imaginary parts of the variable. For example,
x = eig(randn(20,20)); whos Name Size Bytes Class Attributes x 20x1 320 double complex
plotto display the real portion as x and the imaginary portion as y, then link the graph to the variable:
linkdatacannot unambiguously identify the graph's data sources, and you must tell it by typing
imag(x)into the Specify Data Source Properties dialog box that displays when you click fix it in the Linked Plot information bar.
To avoid having to type the data source names in the dialog box, you can specify them when you plot:
If you subsequently change values of
or manually, the plot updates accordingly.
Although you can use data brushing on linked plots of complex
data, your brush marks only appear in the plot you are brushing, not
in other plots or in the Variables editor. This is because function
calls, such as
that you specify as data sources are not interpreted when brushing
You can use
linkdata to connect a graph
with scalar, vector and matrix numeric variables of any class (including
if the graphing function can plot it) — essentially any data
See Example 3 for instructions on
linking complex variables. You can also link plots to numeric fields
within structures. You can specify MATLAB expressions as data
sources, for example,
Refreshing data on a linked plot fails if the variables in the
ZDataSource properties, when evaluated, are
incompatible with what is in the current workspace, such that the
ZData are unable to respond. The visual appearance
of the object in the graph is not affected by such failures, so graphic
objects show no indication of broken links. Instead, a warning icon
and the message Failing links appear on the Linked
Plot information bar along with an Edit button
that opens the Specify Data Sources dialog box.
linkdata buffers updates to data and dispatches
them to plots at roughly half-second intervals. This makes data linking
not suitable for smoothly animating changes in data values unless
they are updated in loops that are forced to execute two times per
second or less.
One consequence of buffering link updates is that
not detect changes in data streams it monitors. If you are running
a function that uses
update workspace variables,
linkdata can sometimes
fail to process updates that change values but not the size and class
of workspace variables. Such failures only happen when the function
itself updates the plot.
If you link data sources to graphs that have been brushed, their
brushing marks can change or vanish. This is because the workspace
variables in those graphs now dictate which, if any, observations
are brushed, superseding any brushing annotations that were applied
to their graphical data (
YData, etc.). For more
details, see How Data Linking Affects Data Brushing in the