MATLAB: How to plot the timeseries data such that the plot has gaps where the data is equal to 0 in MATLAB 7.5 (R2007b)

breaksMATLAB

I would like to plot a timeseries data with gaps where the data it equal to 0:
time={'01-Jan-2001';'02-Jan-2001';'03-Jan-2001';'04-Jan-2001';'05-Jan-2001';'06-Jan-2001';...
'07-Jan-2001';'08-Jan-2001';'09-Jan-2001';'10-Jan-2001';'11-Jan-2001';'12-Jan-2001';'13-Jan-2001'};
data=[1 2 3 0 5 6 0 6 7 0 7 8 9];
% original timeseries:
tsOriginal=timeseries(data,time);
% plot original timeseries:
figure;
plot(tsOriginal)

Best Answer

To plot your timeseries data such that the plot has gaps where data is 0 you can replace all the zeros in your data with NaNs which do not get plotted:
%copy data from the original timeseries:
dataModified=data;
%replace all the zeros with NaNs
dataModified(data==0)=NaN;
%create a modified timeseries with the new data
tsModified=timeseries(dataModified,time);
%plot the resulting timeseries
figure
plot(tsModified)