I get an error saying 'Array indices must be positive integers or logical values' but i need my value to be a decimal. How do i work around this?

10 ビュー (過去 30 日間)
Hello,
I am trying to writecode on MATLAB that will examine 2 columns from a file and than plot the data with its mean and standard deviaiton. Two graphs where it just the data and another two with the standard deviaiton and mean. I can get the the first two plots to get drawn seperately how i like but when i try to plot the standard devaition it gives the error 'Array indices must be positive integers or logical values' but i need my number to be a decimal. so there a way around it? and while I'm at it how would i get MATLAB to draw the standard deviation and mean inside of that loop as well.I have included a copy of the file you will need to download and import to matlab if you would like to run the code. Thank You.
clear all
csv = 'ON_02XXXXX_daily_hydrometric.csv'; %calls the file
data = readtable(csv)
for loop = 1:2
clear y
clear y_minus_reverse
clear y_minus
clear std_y
clear y_plus
clear water_data
if loop == 1
col_data = data.WaterLevel_NiveauD_eau_m_; %assigned waterdepth as the column in the table
elseif loop == 2
col_data = data.Discharge_D_bit_cms_; %assigned waterdepth as the column in the table
end
water_data = col_data; %reads the table od csv
water_data(find(isnan(water_data)))= []
k = length(water_data);
avg = mean(transpose(water_data))* ones(1,k); %Mean of the transpose matrix (waterdepth)
std = std(water_data); %standard deviation for the waterdepth
stdplus = transpose(water_data)+std;
stdminus = transpose(water_data)-std;
y = water_data
x = 1:1:length(y);
subplot(2,2,loop)
plot(x,y,'b')
% Legend, Title, axis labels.
legend('original data')
title('HUMBER RIVER AT ELDER MILLS ID: 02HC025');
xlabel('Historical Daily Water Level (Day) ');
ylabel('Water Levels (m)')
patch([1:1:k k:-1:1],[stdplus, stdminus(g)], 'b','facealpha',0.5)
xlabel('days')
ylabel('waterdepth')
title('Standard Deviation of Magnetawn River')
legend('average')
end
  1 件のコメント
dpb
dpb 2019 年 11 月 28 日
patch([1:1:k k:-1:1],[stdplus, stdminus(g)], 'b','facealpha',0.5)
There is no g defined above...looks to me like should just be
patch([1:1:k k:-1:1],[stdplus, stdminus], 'b','facealpha',0.5)
It would be simpler to just use the column indices of the columns desired (or use the variablenames property with the string representation syntax) in the loop to address the table data instead of creating duplicated variables of the same name that are different.

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

採用された回答

Star Strider
Star Strider 2019 年 11 月 28 日
I cannot run your code, and I am not certain what you are doing.
I do not see that you have defined ‘g’ anywhere, so that may be the problem.
Try this (assuming row vectors):
patch([1:1:k k:-1:1],[stdplus, stdminus], 'b','facealpha',0.5)
or perhaps this:
patch([1:1:k k:-1:1],[stdplus, fliplr(stdminus)], 'b','facealpha',0.5)
If the ‘std*’ arrays are column vectors instead:
patch([1:1:k k:-1:1],[stdplus; flipud(stdminus)], 'b','facealpha',0.5)
  2 件のコメント
Yousaf Ijaz
Yousaf Ijaz 2019 年 11 月 28 日
the reason you can't run the code is becasue you dont have the file, here is the file. I forogot to define the g but now that I did it wont work becasue the problem is that the line where I get it to calculate the std. you can run the code now with the file after you import it in to matlab.
Star Strider
Star Strider 2019 年 11 月 28 日
First, you are ‘overshadowing’ the std function by creating a variable of the same name in the first iteration of the loop:
std = std(water_data); %standard deviation for the waterdepth
Simply renaming it (I chose ‘stdwd’) solves the problem completely:
stdwd = std(water_data); %standard deviation for the waterdepth
stdplus = transpose(water_data)+stdwd;
stdminus = transpose(water_data)-stdwd;
and using my code for the patch call:
patch([1:1:k k:-1:1],[stdplus, fliplr(stdminus)], 'b','facealpha',0.5)
it runs withouit error and produces this figure:
I get an error saying 'Array indices must be positive integers or logical values' but i need my value to be a decimal. How do i work around this - 2019 11 28.png
That appears to be the result you want.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2019 年 11 月 28 日

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by