How can I print an MATLAB file programmatically?

22 views (last 30 days)
The File -> Print option in the MATLAB editor allows me to print the currently open file. I want to be able to print a file from within a MATLAB script or function.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Jun 2009
The ability to programmatically print MATLAB files is not available in MATLAB.
To work around this issue, create a COM server running Microsoft Word in MATLAB, open the respective file in Word, and then call a VBA macro that prints the file to the installed printer. The macro inserts the current date and time, the name of the MATLAB file, and the Page number into the header of the Word document that prints out.
To use the workaround, complete the following steps:
1. Download the attached files -
InsertHeaderMacro.txt
print_m_file.m
2. Define a new macro in Word by navigating to Tools-> Macro -> Record New Macro. In the 'Macro Name' box, type the name 'add_my_header'. Copy and paste the entire macro from 'InsertHeaderMacro.txt'.
3. Run the file 'print_m_file.m' with the name of the MATLAB file that you want to print as an argument in the form of a string. For example, execute the following command in the MATLAB command window:
print_m_file('myMfile.m')
where myMfile.m is to be replaced by the name of your MATLAB file.
The workaround is tested for Word 2003 and MATLAB 7.6 (R2008a).

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!