error using patch vectors must be same length

4 ビュー (過去 30 日間)
Payaam Khalid
Payaam Khalid 2022 年 12 月 5 日
コメント済み: Payaam Khalid 2022 年 12 月 5 日
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
Walter Roberson 2022 年 12 月 5 日
I was seeing that error in your code, but when I try your code now, it works.
Payaam Khalid
Payaam Khalid 2022 年 12 月 5 日
yeah it works for me now as well thanks

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

回答 (1 件)

Voss
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).

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by