if or for loop

6 次查看(过去 30 天)
Arif Hoq
Arif Hoq 2021-10-13
Hello,
I am confused about this issue. i have an excel file with 2 colomn. first column is the time(hours) and second column is the power. like:
time= 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
power= 0,0,0,0,0,12,40,100,200,300,500,800,700,650,600,500,400,200,0,0,0,0,0,0
Now, I want to implement,
First: whenever the value of 'power' is greater than 0, then it will show the time (i.e 6)
Last: whenever the value of 'power' is greater than 0, then it will show the time (i.e 19)
Please give your suggestion. I have attachded the excel file. Thank you very much.

采纳的回答

Dave B
Dave B 2021-10-13
You can find the index of the first and last instance of power greater than 0 using the find function, then put those indices right into time to get the values:
time= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
power= [0,0,0,0,0,12,40,100,200,300,500,800,700,650,600,500,400,200,0,0,0,0,0,0];
time(find(power>0,1,'first'))
ans = 6
time(find(power>0,1,'last'))
ans = 18
  2 个评论
Arif Hoq
Arif Hoq 2021-10-13
It's working. Thanks a lot Dave.
Image Analyst
Image Analyst 2021-10-13
Then please "Accept this answer". Thanks in advance.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by