MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)

       

In this article, I will give an example of how to generate a Word document from an existing template.

In order for this to work, you will have to place the template into your current MATLAB folder (use the command “pwd” to know what that is).
Here is what the empty Word template looks like:

empty word template used to generate Word report from MATLAB.

We will use the following functions that I developed in the previous articles to make this easier:

  1. fopenWord: open the Word file and get the fid structure to reference the file
  2. navigateWord: move the cursor to the desired location on the template
  3. writeData: write data at the location of the cursor
  4. newLine: go to the next line
  5. fcloseWord: close the Word file
currentDirectory = pwd;
wordFile = 'generatedWordFile.docx';
copyfile([currentDirectory '/wordTemplate.docx'], [currentDirectory '/' wordFile])
c = clock;
year = c(1);
month = c(2);
day = c(3);
hour = c(4);
minute = c(5);
seconds = c(6);
dateFormated = [num2str(day) '/' num2str(month) '/' num2str(year)];
fid = fopenWord(wordFile);
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Text
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
absoluteVerticalPosition = 0;
downwardMovement = 3;
navigateWord(fid, 'down', downwardMovement);
font.name = 'Arial';
font.size = 10;
font.bold = 0;
text = 'Cumque pertinacius ut legum gnarus accusatorem flagitaret atque sollemnia, doctus id Caesar libertatemque superbiam ratus tamquam obtrectatorem audacem excarnificari praecepit, qui ita evisceratus ut cruciatibus membra deessent, inplorans caelo iustitiam, torvum renidens fundato pectore mansit inmobilis nec se incusare nec quemquam alium passus et tandem nec confessus nec confutatus cum abiecto consorte poenali est morte multatus. et ducebatur intrepidus temporum iniquitati insultans, imitatus Zenonem illum veterem Stoicum qui ut mentiretur quaedam laceratus diutius, avulsam sedibus linguam suam cum cruento sputamine in oculos interrogantis Cyprii regis inpegit.';
writeData(fid, text, 'Normal', font);
absoluteVerticalPosition = absoluteVerticalPosition + downwardMovement;
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Table
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
downwardMovement = 3;
navigateWord(fid, 'down', downwardMovement);
navigateWord(fid, 'left', 1);
writeData(fid, dateFormated, 'Normal', font);
navigateWord(fid, 'right', 1);
writeData(fid, 'Jacob Sapir', 'Normal', font);
absoluteVerticalPosition = absoluteVerticalPosition + downwardMovement;
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Section
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
downwardMovement = 1;
navigateWord(fid, 'down', downwardMovement);
writeData(fid, 'Second Section', 'section');
absoluteVerticalPosition = absoluteVerticalPosition + downwardMovement;
newLine(fid)
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Subsection
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
writeData(fid, 'First Subsection', 'subsection');
newLine(fid)
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
% Text
%−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
text = 'Cumque pertinacius ut legum gnarus accusatorem flagitaret atque sollemnia, doctus id Caesar libertatemque superbiam ratus tamquam obtrectatorem audacem excarnificari praecepit, qui ita evisceratus ut cruciatibus membra deessent, inplorans caelo iustitiam, torvum renidens fundato pectore mansit inmobilis nec se incusare nec quemquam alium passus et tandem nec confessus nec confutatus cum abiecto consorte poenali est morte multatus. et ducebatur  ntrepidus temporum iniquitati insultans, imitatus Zenonem illum veterem Stoicum qui ut mentiretur quaedam laceratus diutius, avulsam sedibus linguam suam cum cruento sputamine in oculos interrogantis Cyprii regis inpegit.';
writeData(fid, text, 'Normal', font);
fcloseWord(fid);
winopen(wordFile);

This script executes a copy-paste of your template document, renames it as “generatedWordFile.docx,” and writes the data. The variable “absoluteVerticalPosition” has no use in this script and can be removed; however, it can be used to find the absolute vertical position of your cursor.

The end result is the following document:

word file generated from MATLAB automatically