MATLAB Community

MATLAB, community & more

Creating an M-file from selections in the Command History

Getting started writing an M-file can be difficult sometimes. It’s akin to writing a term paper. That first bit is always difficult to write.

Often when I am writing M code, I start in the Command Window, rather than the Editor, to help get the initial part of the code working in a more interactive fashion. In the Command Window I can play around with the code until I get it right. I then go back later and put the code into a function in the Editor. But instead of copying code from the Command Window or the Command History and pasting it in the Editor, I use the Command History context menu: “Create M-File”.

History Create M file

The “Create M-File” context menu item opens an Editor window and pre-populates the new file with any selected commands from the Command History.


The next time you find that you are working in this mode and you are ready to commit your code to a file, try using the “Create M-file” context menuitem from the Command History. This is easier and quicker than using copy/paste or re-typing the code.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.