"Subscript indices must either be real positive integers or logicals" error while using trapz function
1 view (last 30 days)
Show older comments
ishita agrawal on 19 May 2018
Commented: Image Analyst on 19 May 2018
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)));
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?
Ameer Hamza on 19 May 2018
Edited: Ameer Hamza on 19 May 2018
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))
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.
See the FAQ about your error: http://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F
And see this very short tutorial on how to format your code: format your code
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!