if or for loop

2 views (last 30 days)
Arif Hoq
Arif Hoq on 13 Oct 2021
Commented: Image Analyst on 13 Oct 2021
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.

Accepted Answer

Dave B
Dave B on 13 Oct 2021
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 Comments
Arif Hoq
Arif Hoq on 13 Oct 2021
It's working. Thanks a lot Dave.
Image Analyst
Image Analyst on 13 Oct 2021
Then please "Accept this answer". Thanks in advance.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!