Where any issue of fact is joined on any indictment, and any material witness for the defendant resides out of the state, or, residing within the state, is sick or infirm or is about to leave the state, such defendant may apply in writing to the court in term time, or the judge thereof in vacation, for a commission to examine such witness upon interrogatories thereto annexed, and such court or judge may grant the same, and order what and for how long a time notice shall be given the prosecuting attorney before the witness shall be examined.