MATLAB - best way to dynamically update a line handles' XData and YData?

前端 未结 1 751
庸人自扰
庸人自扰 2020-12-18 03:35

I am collecting data and plotting that data in real time. The data are produced by a motion capture system. I have one class DynamicDataset that is just a wrapp

相关标签:
1条回答
  • 2020-12-18 04:32

    The amount of data you're handling in each update, is large (although only a single point is actually changing), making your code O(N^2).

    By using a second lineseries to build up a large group of data, you can alternate between adding every point to a short "active" line, and infrequently adding large blocks to the main lineseries. While this doesn't exactly avoid O(N^2), it lets you reduce the constant significantly.

    If you do this, remember to overlap the "old" lineseries and "active" lineseries by one point, so that they connect.

    Essentially:

        function updatePlot(obj, propNum)
            X = get(obj.LHactive(propNum), 'XData');
            Y = get(obj.LHactive(propNum), 'YData');
    
            X(end+1) = obj.(dynProps{propNum}).newestData(1);
            Y(end+1) = obj.(dynProps{propNum}).newestData(2);
    
            if numel(X) > 100
                Xold = [get(obj.LH(propNum), 'XData'); X(2:end)];
                Yold = [get(obj.LH(propNum), 'YData'); Y(2:end)];
                set(obj.LH(propNum), 'XData', Xold, 'YData', Yold);
    
                X = X(end);
                Y = Y(end);
            end
    
            set(obj.LHactive(propNum), 'XData', X, 'YData', Y);
        end
    
    0 讨论(0)
提交回复
热议问题