MATLAB: Any suggestion for the method I should use for calculating the sum of the event which has zero values and its duration with matlab

excel vba

Hi! I need a suggestion about which method I should use in doing this task. Before, I use Excel VBA to do this and it worked well. Then, I have to change my method to Matlab and I got confused as I am really new in this programming software. I have a data in the form of table, consist of datetime and flow (numeric). The flow is dynamic (like the plot in peak analysis), as it has both zero and non zero values in it. I need to calculate the total events of the condition with zero values (dry period) and the duration of each events. Do you have any suggestion for me to do it in matlab? Here is what I do in Excel VBA (picture attached)
1. I add one column contain sequence number from the first row until the last row beside the datetime data and flow data.
2. I filter the data based on the flow. If it is not zero, I delete the flow and other data in its row.
3. Now, the sequence number may not be in order form because some have been deleted. From this sequence number, I then make a column more beside sequence number column for unique number. The first row would be'1', and if it is still in sequenced form, it remains 1, but if it jumps to three number above or more, it should be '2' and this pattern continue until the last row.
4. After I got all the unique number which represents the event I need, I use pivot table to find maximum and minimum datetime of each event, then I substract it. That way I can find the duration of each event.
Do you have any suggestion for me to do it in matlab (with matlab code?) or should I also do the same as in ExcelVBA? In matlab I also have the table form consist of the data. I really appreciate for your help. Thank you so much in advance.

Best Answer

Looks like you use excel macros, not VBA. In any case, no you should not do the same method at all in matlab. Matlab is a lot more powerful but requires a different way of thinking.
Giving us a file we can play with would have been more useful than screenshots. Anyway, first thing you need to do is import your data in matlab. For that use readtable with appropriate options (possibly none). Something like:
flowtable = readtable('c:\somewhere\somefile.xlsx');
If necessary rename the columns of the table to something useful. In the following I assume the flow column is called Flow. You can then detect dry periods, their beginnings and ends:
dryperiod = flowtable.Flow == 0;
transitions = diff([false, dryperiod, false]); %is 1 at the start of a dry period, -1 after the end
drystarts = find(transitions == 1);
dryends = find(transitions == -1) - 1;
You can then build your result table. I'm assuming that your flowtable has a single column named DateTime of type datetime
drytable = table(flowtable.DateTime(drystarts), ...
flowtable.DateTime(dryends), ...
flowtable.DateTime(dryends) - flowtable.DateTime(drystarts), ...
'VariableNames', {'Start', 'End', 'Duration'})