error using patch vectors must be same length
4 次查看(过去 30 天)
显示 更早的评论
my_url = 'https://dd.weather.gc.ca/hydrometric/csv/ON/daily/ON_02HC025_daily_hydrometric.csv';
gauge_data = webread(my_url); % all the data
depth_data = gauge_data.WaterLevel_NiveauD_eau_m_; % just the depth values5
depth_data(isnan(depth_data))=[];
y=depth_data;
x= 1:1:length(y);
% calculate avg & standard deviation
avg_y_scalar = mean(y);
avg_y_vector = avg_y_scalar * ...
ones(1,length(y));
std_y = std(y);
% data + 1 std deviation (above)
y_avg_plus = avg_y_vector + std_y;
% data - 1 std deviation (below)
y_avg_minus = avg_y_vector - std_y;
j=1;
for i = length(y):-1:1
y_avg_minus_reverse(j) =y_avg_minus(i);
j=j+1; % increment j positive
end
% create plot
figure(4)
% plot the data and average plot(x,y,'b-
plot(x,y,'b->',x,avg_y_vector);
patch([1:1:length(y) ...
length(y):-1:1],...
[y_avg_plus y_avg_minus_reverse],...
'b',...
'facealpha',0.05,...% fill colour
'edgecolor','r',...
'edgealpha',0.05) % edge colour
% Legend, Title, axis labels.
legend('original data',...
'average values',...
'standard deviation')
title('Data (avg & std dev)')
xlabel('Sample Number [no units]');
ylabel('Magnitude [units: Quanta]');
Error using patch
Vectors must be the same length.
Error in untitled1500 (line 27)
patch([1:1:length(y) ...
2 个评论
Walter Roberson
2022-12-5
I was seeing that error in your code, but when I try your code now, it works.
回答(1 个)
Voss
2022-12-5
It may be that y_avg_minus_reverse is longer than y, having been defined as longer in a previous run of the script, since the script sets individual elements of y_avg_minus_reverse (elements 1 through length(y)) without re-initializing.
To avoid that possibility, define y_avg_minus_reverse completely, e.g.:
y_avg_minus_reverse = y_avg_minus(end:-1:1);
or:
y_avg_minus_reverse = flip(y_avg_minus);
rather than defining it with the loop you have (or keep your loop, but initialize y_avg_minus_reverse to the proper size before the loop).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!