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

1 view (last 30 days)
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.

Accepted Answer

Guillaume
Guillaume on 24 Oct 2017
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'})
  12 Comments
Guillaume
Guillaume on 26 Oct 2017
Well, the location of the maximum is the second output of max. This will give you the index relative to the start of the dry period. If you want the absolute index, add the relative indices to the event starts:
[peakflow, peakflowlocrel] = arrayfun(@(s, e) max(flowtable.Flow(s:e)), eventStarts, eventEnds);
peakflowlocabs = peakflowlocrel + eventStarts - 1;
Kasih Ditaningtyas Sari Pratiwi
Edited: Kasih Ditaningtyas Sari Pratiwi on 27 Oct 2017
Thank you so much Guillaume. You saved my life again! :) I use peakflowlocabs to then draw the max flow in original data and it works. But actually I do not understand why we have to add "eventStarts - 1" there in looking for "peakflowlocabs". Could you please explain it? Thank you again for your help

Sign in to comment.

More Answers (0)

Categories

Find more on Time Series in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!