Generate PDF, HTML, DOC file of your MATLAB code, figures, using MATLAB itself

Many times (almost every times, actually!) we use external methods or softwares to get our MATLAB code be converted to publish ready formats like, PDF, HTML or DOC. But MATLAB developers were smart enough, to provide some options so that a developer can export/publish his code into another format, which may be printer or web -friendly.
That option in MATLAB is known by the name "publish" command.

By default you can use the command like this:
publish('hello.m'); %Here hello.m is the matlab file you want to publish

It will generate a HTML file inside a folder named "html" in the current working directory. The HTML file will have the same name as your MATLAB (.m) file is.

But there are many other formats too that MATLAB supports.
Namely, PDF, DOC, XML, PPT, TEX & HTML being the default one.

For creating a PDF file for your MATLAB code, use the command:

publish('hello.m', 'pdf');

For DOC File,
publish('hello.m', 'doc');

For PPT FIle.
publish('hello.m', 'ppt') 

Suppose in a scenario, you require to generate the report through a script in which you just want to get the location of the report with the file name, you need to just put a variable before giving the publish command.
Now the variable 'x' will be a string variable, which contains value like this:
x =

If you want to control other parameters of the documents like, max height, width, max no. of lines etc. You can define a structure variable and pass on to the parameters of publish command with the file name.

op= struct('format','doc','outputDir','C:\XYZ', ...., ...., ...., 'Name', Value')
See help for a detailed view of options.
And pass that like:

If you are using Academic version of MATLAB, then there would be a watermark in your published file. 

1 comment: