"Subscript indices must either be real positive integers or logicals" error while using trapz function

Hello, I am using following code to calculate area under the peak.
D = amplitude(:);
l = 59;
chg(1) = 1;
chg(2) = l;
for k1 = 1:numel(chg)-1
segment_area_1k1(a,j) = trapz(chg(k1:k1+1), D(chg(k1:k1+1)));
end
end
The code upon run shows error "subscript indices must either be real positive integers or logicals". I have attached amplitude data for reference. Can anyone solve this problem?

답변 (2개)

Ameer Hamza
Ameer Hamza 2018년 5월 19일
편집: Ameer Hamza 2018년 5월 19일
What are the values of a and j? They must be a positive integer. Also if you just want to calculate the area under the curve, then pass an entire vector to trapz()
area = trapz(D);
If you want to get segmented area, you can avoid for loop altogether. A better approach is to do it like this
segmentedArea = diff(cumtrapz(D))

댓글 수: 4

a and j are positive integers.
In that case, it is quite strange, because your code works fine with positive values of a and j when I run it. Have you tried the functions I wrote in my answer.
Actually, this code worked perfectly for other datasets. This time, it is working for some values in the dataset and then this error occurred.
There is something I want to ask. When I tried your code (trapz(D)), the value of calculated area was different than what I calculated using my code. I couldn't understand the reason.
Give your complete code, including the code where you read in the data file, and your complete error message, including line numbers and ALL the red text.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

질문:

2018년 5월 19일

댓글:

2018년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by