how to use a 'loop' for certain counts
Mostrar comentarios más antiguos
I have 15 areas in my power data if i plot. want to calculate each area by using trapz function. can calculate each area manually(is shown in the code), but want to find these area automatically. maybe loop can be the solution.
there are 16 zero crossing indices. so from 1:16, there are 15 areas. used this below written loop, but it's showing the final value. I want to get every area from 1 to 16.
i=0;
for i=1:length(zeroaxes)
time(i:zeroaxes(i))=time(i:zeroaxes(i));
power(i:zeroaxes(i),1)=power(i:zeroaxes(i));
areaone=trapz(time,power);
end
would you please let me know any way ? attached the mat file.
thank you very much
Code(area calculated manually):
A=load('power.mat');
power=A.power;
time=1:length(power);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices
zeroaxes = zci(power);
%% Area Calculation
time1=time(1:zeroaxes(2)); % time for area1
power1=power(1:zeroaxes(2)); % time for area1
area1=trapz(time1,power1); % integrated area1
time2=time(zeroaxes(2):zeroaxes(3));
power2=power(zeroaxes(2):zeroaxes(3));
area2=trapz(time2,power2);
time3=time(zeroaxes(3):zeroaxes(4));
power3=power(zeroaxes(3):zeroaxes(4));
area3=trapz(time3,power3);
time4=time(zeroaxes(4):zeroaxes(5));
power4=power(zeroaxes(4):zeroaxes(5));
area4=trapz(time4,power4);
time5=time(zeroaxes(5):zeroaxes(6));
power5=power(zeroaxes(5):zeroaxes(6));
area5=trapz(time5,power5);
time6=time(zeroaxes(6):zeroaxes(7));
power6=power(zeroaxes(6):zeroaxes(7));
area6=trapz(time6,power6);
time7=time(zeroaxes(7):zeroaxes(8));
power7=power(zeroaxes(7):zeroaxes(8));
area7=trapz(time7,power7);
time8=time(zeroaxes(8):zeroaxes(9));
power8=power(zeroaxes(8):zeroaxes(9));
area8=trapz(time8,power8);
time9=time(zeroaxes(9):zeroaxes(10));
power9=power(zeroaxes(9):zeroaxes(10));
area9=trapz(time9,power9);
time10=time(zeroaxes(10):zeroaxes(11));
power10=power(zeroaxes(10):zeroaxes(11));
area10=trapz(time10,power10);
time11=time(zeroaxes(11):zeroaxes(12));
power11=power(zeroaxes(11):zeroaxes(12));
area11=trapz(time11,power11);
time12=time(zeroaxes(12):zeroaxes(13));
power12=power(zeroaxes(12):zeroaxes(13));
area12=trapz(time12,power12);
time13=time(zeroaxes(13):zeroaxes(14));
power13=power(zeroaxes(13):zeroaxes(14));
area13=trapz(time13,power13);
time14=time(zeroaxes(14):zeroaxes(15));
power14=power(zeroaxes(14):zeroaxes(15));
area14=trapz(time14,power14);
time15=time(zeroaxes(15):zeroaxes(16));
power15=power(zeroaxes(15):zeroaxes(16));
area15=trapz(time15,power15);
Respuesta aceptada
Más respuestas (1)
Karthik P.V
el 14 de Dic. de 2021
Can you try with this for loop?
for i=1:length(zeroaxes)
try
eval(['time_',num2str(i),'=time(i:zeroaxes(i))']);
eval(['power_',num2str(i),'=power(i:zeroaxes(i))']);
power(i:zeroaxes(i),1)=power(i:zeroaxes(i));
eval(['area_',num2str(i),'=trapz(time_',num2str(i),',power_',num2str(i),')']);
catch
continue;
end
end
5 comentarios
Arif Hoq
el 14 de Dic. de 2021
Rik
el 14 de Dic. de 2021
In addition to what I wrote in my answer about numbered variables, you also shouldn't be using length. It does max(size(___)). I have never found a situation where this was actually the intended syntax, instead of using numel or (size with a second input argument).
Karthik P.V
el 15 de Dic. de 2021
Stephen23
el 21 de Dic. de 2021
Using numbered variable names is a sign that you are doing something wrong.
Accessing variable names dynamically is slow, complex, inefficient, liable to bugs, and difficult to debug.
The simpler and more efficient MATLAB approach is to use indexing.
Arif Hoq
el 21 de Dic. de 2021
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!