Optimized Chart Class for Displaying Variable Number of Lines
This example shows how to optimize a chart class for displaying a variable number of lines. It reuses existing line objects, which can improve the performance of the chart, especially if the number of lines does not change frequently. For a simpler version of this chart without the optimization, seeChart Class with Variable Number of Lines.
The chart displays as many lines as there are columns in theYData
matrix, with circular markers at the local extrema. The following code demonstrates how to:
Define two properties called
PlotlineArray.
和ExtremaLine
that store the objects for the lines and the markers, respectively.Implement a
setup
method that initializes theExtremaLine
目的。Implement an
update
method that gets the size of thePlotlineArray.
, and then adds or subtracts objects from that array according to the number of columns inYData
.
To define the class, copy this code into the editor and save it with the nameOptimLocalExtremaChart.m
in a writable folder.
classdefOptimLocalExtremaChart < matlab.graphics.chartcontainer.ChartContainer% c = OptimLocalExtremaChart('XData',X,'YData',Y,Name,Value,...)% plots one line with markers at local extrema for every column of matrix Y.% You can also specify the additonal name-value arguments, 'MarkerColor'% and 'MarkerSize'.特性XData(:,1) double= NaN YData(:,:) double= NaN MarkerColor{validatecolor}= [1 0 0] MarkerSize(1,1)双= 5end特性(Access = private,Transient,NonCopyable) PlotLineArray(:,1) matlab.graphics.chart.primitive.LineExtremaLine(:,1) matlab.graphics.chart.primitive.Lineendmethods(Access = protected)functionsetup(obj) obj.ExtremaLine = matlab.graphics.chart.primitive.Line(...'Parent', obj.getAxes(),'Marker','o',...'MarkerEdgeColor','none','LineStyle',' none');endfunction更新(obj)% Get the axesax =木屐xes(obj);% Create extra lines as neededp = obj.PlotLineArray; nPlotLinesNeeded = size(obj.YData, 2); nPlotLinesHave = numel(p);forn = nPlotLinesHave+1:nPlotLinesNeeded p(n) = matlab.graphics.chart.primitive.Line('Parent', ax,...'SeriesIndex',n,'LineWidth', 2);end% Update the linesforn = 1:nplotlinesneeded p(n).xdata = obj.xdata;p(n).ydata = obj.ydata(:,n);end% Delete unneeded linesdelete(p((nPlotLinesNeeded+1):numel(p))) obj.PlotLineArray = p(1:nPlotLinesNeeded);% Replicate x-coordinate vectors to match size of YDatanewx = repmat(obj.XData(:),1,size(obj.YData,2));% Find local minima and maxima and plot markerstfmin = islocalmin(obj.ydata,1);tfmax = islocalmax(obj.ydata,1);obj.extremaline.xdata = [newx(tfmin);newx(tfmax)];obj.extremaline.ydata = [obj.ydata(tfmin);obj.ydata(tfmax)];obj.extremaline.markerfacecolor = obj.markercolor;obj.extremaline.markersize = obj.markersize;% Make sure the extrema are on topuistack(obj.ExtremaLine,'top');endendend
After saving the class file, you can create an instance of the chart. For example:
x = linspace(0,2)'; y = cos(5*x)./(1+x.^2); c = OptimLocalExtremaChart(“XData”,X,'YData',y);
Now, create afor
循环在每次迭代时添加到绘图的附加线。图表对象保留所有现有行,并为每个添加一个addital行i
.
fori=1:10 y = cos(5*x+i)./(1+x.^2); c.YData = [c.YData y];end