How to solve NaN when using trapz?

Hello all,
I am trying to do a numerical integration using trapz from a excel data. But I am getting NaN. Please provide me with some guidence.
My code:
[num data raw] = xlsread('Flowratevel.xlsx');
col_1 = num(:,1);
col_2 = num(:,2);
col_3 = num(:,3);
col_4 = num(:,4);
col_5 = num(:,5);
col_6 = num(:,6);
col_7 = num(:,7);
col_8 = num(:,8);
col_9 = num(:,9);
col_10 = num(:,10);
A1 = pi*(num(:,1)).^2;
Q1 = A1.*num(:,2);
Int1 = trapz(A1, num(:,2));
A2 = pi*(num(:,3)).^2;
Q2 = A2.*num(:,4);
Int2 = trapz(A2, num(:,4));
A3 = pi*(num(:,5)).^2;
Q3 = A3.*num(:,6);
Int3 = trapz(A3, num(:,6));
A4 = pi*(num(:,7)).^2;
Q4 = A4.*num(:,8);
Int4 = trapz(A4, num(:,8));
A5 = pi*(num(:,9)).^2;
Q5 = A5.*num(:,10);
Int5 = trapz(A5, num(:,10));

 Accepted Answer

Torsten
Torsten on 21 Jan 2022
Edited: Torsten on 21 Jan 2022
[num data raw] = xlsread('Flowratevel.xlsx');
for i=1:size(num,2)
Num{i} = num(:,i)(~isnan(num(:,i)));
end
A1 = pi*Num{1}.^2;
Q1 = A1.*Num{2};
Int1 = trapz(A1, Num{2})
A2 = pi*Num{3}.^2;
Q2 = A2.*Num{4};
Int2 = trapz(A2, Num{4})
A3 = pi*Num{5}.^2;
Q3 = A3.*Num{6};
Int3 = trapz(A3, Num{6})
A4 = pi*Num{7}.^2;
Q4 = A4.*Num{8};
Int4 = trapz(A4, Num{8})
A5 = pi*Num{9}.^2;
Q5 = A5.*Num{10};
Int5 = trapz(A5, Num{10})

3 Comments

Hello @Torsten,
Thank you for the answer. But when I run the code I am getting the following error.
'Indexing with parentheses '()' must appear as the last operation of a valid indexing expression.'
In line "Num{i} = num(:,i)(~isnan(num(:,i)));"
Can you please let me know how to slove this.
Torsten
Torsten on 24 Jan 2022
Edited: Torsten on 24 Jan 2022
I don't know why it doesn't work as above, but alternatively try
for i = 1:size(num,2)
%Num{i} = num(:,i)(~isnan(num(:,i)));
Col = num(:,i);
Num{i} = Col(~isnan(Col));
end
Hello @Torsten
Thank you it worked.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!