{"id":202,"date":"2008-04-14T05:58:31","date_gmt":"2008-04-14T10:58:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/04\/14\/a-link-to-the-data\/"},"modified":"2017-06-07T22:07:36","modified_gmt":"2017-06-08T02:07:36","slug":"a-link-to-the-data","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/04\/14\/a-link-to-the-data\/","title":{"rendered":"A Link to the Data"},"content":{"rendered":"<p>In Release 2008a, we added the ability to <a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/index.html?\/access\/helpdesk\/help\/techdoc\/data_analysis\/brh7_h0-1.html\"> link plots to source data<\/a>. With this feature, you tie a plot to a particular value in the workspace. When that value changes, the plot is automatically updated, with no further intervention on your part.<\/p>\n<p>For this release, we&#8217;ve made a <a href=\"https:\/\/www.mathworks.com\/support\/2008a\/matlab\/7.6\/demos\/LinkedPlotsAndDataBrushing.html\">short video tutorial<\/a> explaining how to use this feature, with a particularly useful application: <em>watching your variables change during a debugging session<\/em>. The second half of the video covers another new feature, <a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/index.html?\/access\/helpdesk\/help\/techdoc\/data_analysis\/brh7_p3-1.html\">data brushing<\/a>, which is a topic for a later post.<\/p>\n<p>One use not covered in the video is for observing real-time data. A while back I wrote a program to monitor incoming bytes from the serial port and update a graph with that data: a serial &#8220;oscilliscope&#8221;, if you will. In order to achieve smooth scrolling, instead of re-plotting the data, I modified the axes&#8217;<a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/index.html?\/access\/helpdesk\/help\/techdoc\/creating_plots\/f10-1460.html#bre03df-1\"> <tt>Xdata<\/tt> and <tt>Ydata<\/tt><\/a> properties whenever there were <a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/index.html?\/access\/helpdesk\/help\/techdoc\/matlab_external\/bytesavailablefcn.html\"><br \/>\n<tt>BytesAvailable<\/tt><\/a>. This was a cumbersome hack.<\/p>\n<p>With this new feature, I can just use the data callback function to update my variable in the workspace and let the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/plot.html\"><tt>plot<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/linkdata.html\"><tt>linkdata<\/tt><\/a> objects do the heavy lifting.<\/p>\n<p>The following real-time example uses the <tt>yahoo<\/tt> function from the Datafeed Toolbox. The same principles can be applied to any asynchronous callback in MATLAB, such as the above-mentioned BytesAvailableFcn for a <tt>serial<\/tt> object, most of the objects from the Data, Instrument, or Image Acquisition Toolboxes, Simulink and the target link toolboxes, or even a GUI control callback. This code uses a <tt>timer<\/tt> object to query AT&amp;T&#8217;s stock price every 10 seconds, and adds that value to an array in the workspace.<\/p>\n<p>Because the plot is linked to the data, the plot automatically updates to reflect the new value, thus giving us a real-time stock ticker.<\/p>\n<div class=\"content\">\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">close <span style=\"color: #a020f0;\">all<\/span>;delete(timerfindall);clear <span style=\"color: #a020f0;\">all<\/span>;\r\nConnect = yahoo; <span style=\"color: #228b22;\">%Datafeed Toolbox function<\/span>\r\nval=[];\r\ntime=[];\r\nt = timer(<span style=\"color: #a020f0;\">'TimerFcn'<\/span>,<span style=\"color: #0000ff;\">...<\/span>\r\n    [<span style=\"color: #a020f0;\">'data = fetch(Connect, ''t'',''last'');'<\/span><span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'val(end+1) = data.last;'<\/span><span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'time(end+1)=rem(now,1);'<\/span>] ,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Period'<\/span>, 10,<span style=\"color: #a020f0;\">'ExecutionMode'<\/span>,<span style=\"color: #a020f0;\">'fixedRate'<\/span>);\r\nstart(t) <span style=\"color: #228b22;\">%start the timer<\/span>\r\n<span style=\"color: #228b22;\">%unambiguously set the source data<\/span>\r\nplot(time,val,<span style=\"color: #a020f0;\">'XDataSource'<\/span>,<span style=\"color: #a020f0;\">'time'<\/span>,<span style=\"color: #a020f0;\">'YDataSource'<\/span>,<span style=\"color: #a020f0;\">'val'<\/span>)\r\ntitle(<span style=\"color: #a020f0;\">'Price of AT&amp;T'<\/span>)\r\nlinkdata <span style=\"color: #a020f0;\">on<\/span> <span style=\"color: #228b22;\">%link the the data to the plot<\/span><\/pre>\n<\/div>\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_linked_plots\/linked_plot_yahoo.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_yahoo_small.png\" alt=\"AT&amp;T stock price over a few minutes\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/a><\/div>\n<p>Watching a stock fluctuate over a few minutes isn&#8217;t generally exciting, but you get the idea. What&#8217;s the big deal, then? I think this feature is best for when we don&#8217;t know when the data is going to change (but that it will), or when you want change the data iteratively and interactively without having to do a lot of retyping, as in the following example.<\/p>\n<p>I like linking for situations where one is tinkering around with a variable in the command window. For me this is usually applying different filters to some data until I get the parameters just right. For instance, let&#8217;s say I have some data and I want to tweak filter coefficients until I get the desired response on my data. The following example requires the Signal Processing Toolbox.<\/p>\n<p>First let&#8217;s set up the data:<\/p>\n<div class=\"content\">\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">t = linspace(0,6*pi,100);\r\nx = sawtooth(t); <span style=\"color: #228b22;\">%sawtooth wave - Signal Processing Toolbox function<\/span>\r\ny = x;\r\nplot(abs(fft(y)))<\/pre>\n<\/div>\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_sawtooth.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_sawtooth_small.png\" alt=\"Sawtooth frequency response\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/a><\/div>\n<p>This time let&#8217;s use the GUI to set the <tt>YDataSource<\/tt> by clicking the link button: <img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/tool_plot_linked.png\" alt=\"\" \/>. Then, click the &#8220;fix it&#8221; link (1) in the message bar, and then enter a <tt>fft<\/tt> expression in the <tt>YDataSource<\/tt> field (2).<\/p>\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_ydatasource.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_ydatasource_small.png\" alt=\"Linked plot GUI\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/a><\/div>\n<p>Now let&#8217;s try out a 2nd-order <tt>butter<\/tt>worth filter. We won&#8217;t have to manually re-FFT or re-plot the new output:<\/p>\n<div class=\"content\">\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[a,b]=butter(2,0.1); <span style=\"color: #228b22;\">%Signal Processing Toolbox function<\/span>\r\ny = filter(a,b,x);<\/pre>\n<\/div>\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_butter2.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_butter2_small.png\" alt=\"Linked plot after 2nd order butterworth\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/a><\/div>\n<p>Close, but not quite, so I&#8217;ll try upping the order to 4:<\/p>\n<div class=\"content\">\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[a,b] = butter(4,0.1);\r\ny=filter(a,b,x)<\/pre>\n<div>\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_butter4.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/\/michael_katz_linked_plots\/linked_plot_butter4_small.png\" alt=\"Linked plot after 4th order butterworth\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/a><\/div>\n<p>Good enough! Of course I could have just as easily put the whole <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/filter.html\"><tt>filter<\/tt><\/a> expression in my <tt>YDataSource<\/tt> instead, saving me an extra line.<\/p>\n<p>I invite you to let us know what other creative uses for this you come up with. Do I hear animation?<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Release 2008a, we added the ability to  link plots to source data. With this feature, you tie a plot to a particular value in the workspace. When that value changes, the plot is automatically&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/04\/14\/a-link-to-the-data\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":4662,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/202\/revisions\/4662"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}