A Link to the Data
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 updated, with no further intervention on your part.
For this release, we’ve made a short video tutorial explaining how to use this feature, with a particularly useful application: watching your variables change during a debugging session. The second half of the video covers another new feature, data brushing, which is a topic for a later post.
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 “oscilliscope”, if you will. In order to achieve smooth scrolling, instead of re-plotting the data, I modified the axes’ Xdata and Ydata properties whenever there were
BytesAvailable. This was a cumbersome hack.
With this new feature, I can just use the data callback function to update my variable in the workspace and let the plot and linkdata objects do the heavy lifting.
The following real-time example uses the yahoo 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 serial 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 timer object to query AT&T’s stock price every 10 seconds, and adds that value to an array in the workspace.
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.
close all;delete(timerfindall);clear all; Connect = yahoo; %Datafeed Toolbox function val=; time=; t = timer('TimerFcn',... ['data = fetch(Connect, ''t'',''last'');'... 'val(end+1) = data.last;'... 'time(end+1)=rem(now,1);'] ,... 'Period', 10,'ExecutionMode','fixedRate'); start(t) %start the timer %unambiguously set the source data plot(time,val,'XDataSource','time','YDataSource','val') title('Price of AT&T') linkdata on %link the the data to the plot
Watching a stock fluctuate over a few minutes isn’t generally exciting, but you get the idea. What’s the big deal, then? I think this feature is best for when we don’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.
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’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.
First let’s set up the data:
t = linspace(0,6*pi,100); x = sawtooth(t); %sawtooth wave - Signal Processing Toolbox function y = x; plot(abs(fft(y)))
This time let’s use the GUI to set the YDataSource by clicking the link button: . Then, click the “fix it” link (1) in the message bar, and then enter a fft expression in the YDataSource field (2).
Now let’s try out a 2nd-order butterworth filter. We won’t have to manually re-FFT or re-plot the new output:
[a,b]=butter(2,0.1); %Signal Processing Toolbox function y = filter(a,b,x);
Close, but not quite, so I’ll try upping the order to 4:
[a,b] = butter(4,0.1); y=filter(a,b,x)
Good enough! Of course I could have just as easily put the whole filter expression in my YDataSource instead, saving me an extra line.
I invite you to let us know what other creative uses for this you come up with. Do I hear animation?
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。