On holding the defendant to answer or on a plea of guilty where permitted by law, the magistrate may take from each of the material witnesses examined before him on the part of the people a written undertaking, to the effect that he will appear and testify at the court to which the depositions and statements or case are to be sent, or that he will forfeit the sum of five hundred dollars.