# OpenStax-CNX

You are here: Home » Content » Publishing with MATLAB

### Recently Viewed

This feature requires Javascript to be enabled.

# Publishing with MATLAB

Module by: Serhat Beyenir. E-mail the author

Summary: Basic Publishing with MATLAB

MATLAB includes an automatic report generator called publisher. The publisher publishes a script in several formats, including HTML, XML, MS Word and PowerPoint. The published file can contain the following:

• Commentary on the code,
• MATLAB code,
• Results of the executed code, including the Command Window output and figures created by the code.

## The publish Function

The most basic syntax is publish('file','format') where the m-file is called and executed line by line then saved to a file in specified format. All published files are placed in the html directory although the published output might be a doc file.

## Publishing with Editor

The publisher is easily accessible from the Editor toolbar and file menu:

## Example 1

Write a simple script and publish it in an html file.

Select File > New > Script to create an m-file. Once the editor is opened, type in the following code:

x = [0:6];   % Create a row vector
y = 1.6*x;   % Compute y as a function of x
[x',y']     % Transpose vectors x and y
plot(x,y),title('Graph of y=f(x)'),xlabel('x'),ylabel('f(x)'),grid % Plot a graph

Save the script as publishing.m and select File > Publish. An HTML file is generated as shown in the figure below:

## The Double Percentage %% Sign

The scripts sometimes can be very long and their readability might be reduced. To improve the publishing result, sections are introduced by adding descriptive lines to the script preceded by %%. Consider the following example.

## Example 2

Edit the script created in the example above to look like the code below:

%% This file creates vectors, displays results and plots an x-y graph
x = [0:6];   % Create a row vector
y = 1.6*x;   % Compute y as a function of x
%% Tabulated data
[x',y']     % Transpose vectors x and y
%% Graph of y=f(x)
plot(x,y),title('Graph of y=f(x)'),xlabel('x'),ylabel('f(x)'),grid % Plot a graph

Save the script, a new HTML file is generated as shown in the figure below:

## Summary of Key Points

1. MATLAB can generate reports containing commentary on the code, MATLAB code and the results of the executed code,
2. The publisher generates a script in several formats, including HTML, XML, MS Word and PowerPoint.
3. The Double Percentage %% can be used to creates hyper-linked sections.

## Content actions

### Give feedback:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks