Prevent plot() filling missing data between points

I have the plot belo where x=datenum and y=SPL. You can see, particularly in the second plot down, that there are periods where the data 'jumps'. This is because there are some missing days in the dataset. How do I leave these periods blank rather than having Matlab fill them in automatically? This is sound data so it is not correct to interpolate between the two points like this.
%all subplots use the same approach:
subplot(5,1,1)
X = [dBcalc_goat(:,1)' fliplr(dBcalc_goat(:,1)')];
Y = [dBcalc_goat(:,7)' fliplr(dBcalc_goat(:,10)')];
fill(X,Y,[0.929411764705882 0.694117647058824 0.125490196078431],...
'EdgeColor',[0.929411764705882 0.694117647058824 0.125490196078431]);
hold on
plot(dBcalc_goat(:,1),dBcalc_goat(:,5),'Color',[1 0 0],'LineWidth',2); %RMS level
plot(dBcalc_goat(:,1),dBcalc_goat(:,4),'Color',[ 0 0 0]); %median
plot(dBcalc_goat(:,1),dBcalc_goat(:,7),'Color',[0.929411764705882 0.694117647058824 0.125490196078431]); %5th percentile
plot(dBcalc_goat(:,1),dBcalc_goat(:,10),'Color',[0.929411764705882 0.694117647058824 0.125490196078431]); %95th percentile
xlim([first last]);
xticks([])
ylim([100 140])
xline(winter_19,'--');
xline(spring_19,'--');
xline(summer_20,'--');
xline(autumn_20,'--');

 採用された回答

Matt Gaidica
Matt Gaidica 2021 年 1 月 26 日

0 投票

Use NaN.

5 件のコメント

Louise Wilson
Louise Wilson 2021 年 1 月 26 日
Use it how? Add in times where there are no data with NaN?
Walter Roberson
Walter Roberson 2021 年 1 月 26 日
Or inf.
MATLAB will not draw line segments unless all coordinates for the begining and ending of the line segment are real and finite.
There are, though, some corner cases.
If you use a log plot and some of the data to be represented in log coordinates is negative but other is positive, then the negative parts would correspond to complex in log space, and line segments with them will not be plotted.
But!
If you use a log plot and all of the data to be represented in log coordinates is negative, then even though the negative parts would correspond to complex in log space, MATLAB will plot anyhow, as if the data were not negative. I consider that a bug, but Mathworks considers it a feature. It is mentioned in an obscure place that is not linked to from plot() . Not cool.
Louise Wilson
Louise Wilson 2021 年 1 月 26 日
Great, thank you. Do you know how I would refer to differently named variables in a loop so I don't have to copy the different variables each time?
For example, I have five different sites:
site_var={'dBcalc_noises','dBcalc_tiri'...};
These are variables in my workspace but I don't know how to refer to them without copying and pasting the variable name for each iteration of the loop?
studyperiod=datenum(2019,05,29):datenum(2020,06,8); %add in NaNs for missing data
studyperiod=studyperiod';
for a=1:length(studyperiod)
day_dn=studyperiod(a,1);
[lia,loc]=ismember(day_dn,dBcalc_noises);
if (lia==1)
studyperiod(a,2:11)=dBcalc_noises(loc,2:11);
elseif (lia==0)
studyperiod(a,2:11)=NaN;
end
end
dBcalc_noises=studyperiod;
Walter Roberson
Walter Roberson 2021 年 1 月 26 日
Don't Do That. Use a cell array.
Louise Wilson
Louise Wilson 2021 年 1 月 26 日
Great, I have it now, thanks Walter! I was using the following to fill the space between the upper and lower line before, when I didn't have the NaNs, but now it doesn't work with the NaNs. Do you know how I could fill the space between the plots when there are NaNs in there?
subplot(5,1,1)
X = [dBcalcs.GoatIsland(:,1)' fliplr(dBcalcs.GoatIsland(:,1)')]; %fill gap between percentile lines
Y = [dBcalcs.GoatIsland(:,7)' fliplr(dBcalcs.GoatIsland(:,10)')];
fill(X,Y,[0.929411764705882 0.694117647058824 0.125490196078431],...
'EdgeColor',[0.929411764705882 0.694117647058824 0.125490196078431]);
hold on
plot(dBcalcs.GoatIsland(:,1),dBcalcs.GoatIsland(:,5),'Color',[1 0 0],'LineWidth',2); %RMS level
plot(dBcalcs.GoatIsland(:,1),dBcalcs.GoatIsland(:,4),'Color',[ 0 0 0]); %median
plot(dBcalcs.GoatIsland(:,1),dBcalcs.GoatIsland(:,7),'Color',[0.929411764705882 0.694117647058824 0.125490196078431]); %5th percentile
plot(dBcalcs.GoatIsland(:,1),dBcalcs.GoatIsland(:,10),'Color',[0.929411764705882 0.694117647058824 0.125490196078431]); %95th percentile

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020a

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by