I collect data and draw this data in real time. Data is created by a motion capture system. I have one DynamicDataset class, which is just a wrapper around a matrix with two columns (although it is thinner) with an event notification for new data; another DynamicPlotter class that listens for an event with data addition and dynamically updates the chart. Relevant code fragments:
classdef DynamicDataset < handle properties newestData = []; data = [] end events DataAdded end methods function append(obj, val) obj.data(end+1,:) = val; obj.newestData = val; notify(obj, 'DataAdded'); end end end classdef DynamicPlotter < dynamicprops properties FH %# figure handle AH %# axes handle LH %# array of line handles - may have multiple lines on the plot dynProps = {} %# cell array of dynamic property names - %# use to access individual datasets end methods function obj = DynamicPlotter(props) %# props is a cell array of dynamic %# properties to store information for i = 1:length(props) addprop(obj, props{i}); obj.(props{i}) = DynamicDataset; obj.dynProps = [obj.dynProps props{i}]; addlistener(obj.(props{i}), 'DataAdded', @obj.updatePlot(i)); end obj.createBlankPlot(); end function createBlankPlot(obj) obj.FH = figure; obj.AH = axes; hold all; for i = 1:length(obj.dynProps) obj.LH(i) = plot(nan); %# only used to produce a line handle set(obj.LH(i), 'XData', [], 'YData', []); end end function updatePlot(obj, propNum) X = get(obj.LH(propNum), 'XData'); Y = get(obj.LH(propNum), 'YData'); X(end+1) = obj.(dynProps{propNum}).newestData(1); Y(end+1) = obj.(dynProps{propNum}).newestData(2); set(obj.LH(propNum), 'XData', X, 'YData', Y); end end end
Based on the MATLAB code profile, the set command in updatePlot() pretty expensive. I am wondering if there is a better way to calculate individual points as they become available? Ideally, I would click one point on XData and YData and draw only that point, but I don’t know if this is possible.
Please note that there may be several line objects (for example, several graphs on the same section); plot() takes an axis descriptor as an argument, so it will not consider the properties of previously drawn line descriptors (or is there a way to do this); I was thinking about just doing plot(x,y);hold all; but that would give me separate line handles each time, each of which would correspond to one point.
It may not be possible to make a graph of incoming points faster, but I decided that I would ask.
EDIT . Updated OP with the actual code I'm working with, instead of using a common example for misinterpretation.