Some refactoring.
Some refactoring. Added bag2text to create data for MATLAB.