Thanks Walter for your answer. However, I don't understand much about your recommendation.
I came across with the method in this page: http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/279302
It will involve with a "for" loop to call the dw after getting the output of t and w. It works for me at the moment, but will not be efficient because of the loop, especially when the ode function system is complicated.