"

Appendix A – Publishing and Sharing Code

There are times when you want to share your program with others, either as a report (such as assignment submissions for students) or for collaboration with other programmers. This appendix discusses several ways to share your code and/or its output with others.

A.1 Publishing a Script or Function

The simplest way to create a report of your code and its output that can be viewed by a non-MATLAB user is the publish utility. Many of the downloadable pdfs containing code examples were generated this way. To publish a script, we recommend using the Publish tab in the MATLAB environment:

Open the script that you wish to publish in the editor and bring it to the foreground. If you press the Publish button with default settings, an html file containing your code and its output will be generated. In many cases, as with assignment submissions at our university, a pdf is preferred. To select pdf output

  • use the Publish dropdown menu and select Edit Publishing Options
  • select pdf under Output File Format
  • click Publish

By default, the generated pdf will be in a subfolder of your current directory called HTML, but you have the option to change it. The generated pdf will contain your code, the command window output, and any figures created by the script.

An alternative to using the Publish tab is to execute the publish function from the command line:

>> publish('test_script', 'pdf')

ans =

'C:\Users\jtone\Local Documents\MATLAB\html\test_script.pdf'

This allows you to specify the file type without having to change the options. WARNING: Always do this from the command line – DO NOT PUT IN IN THE SCRIPT FILE. Doing the latter will create an infinite loop, since every time the publish line is reached, the script will execute again.

To publish a function, use publish() from the command line, providing the input arguments using the codeToEvaluate option:

>> publish("parachute_sim", "format", "pdf", "codeToEvaluate", "parachute_sim(4000, 80, 45, 0.8, 1, 5);")

ans =

'C:\Users\jtone\Local Documents\MATLAB\html\parachute_sim.pdf'

In this case, because we are specifying the input arguments using a name-value pair, we must do the same with the file format. We included a semi-colon in the function call, since this function returns a large vector of outputs, which we do not want to be displayed in the pdf file. If you do want the output of the function to be included in the pdf, omit the semi-colon, as in this example, which uses an example function from an earlier chapter:

>> publish("factrl", "format", "pdf", "codeToEvaluate", "f20 = factrl(20)")

The contents of the generated pdf are:

There are some limitations to the publish utility. Most important, it cannot handle user input. For that reason, we discuss some other methods of generating a report of your program and its outputs.

A.2 Creating a pdf Through a Live Script

Another way to generate a pdf of your code and output is to convert to a live script, then export as a pdf. The steps are:

  • select Save as from the File menu
  • select MATLAB live code files (*.mlx) from the dropdown
  • click Save

A live script version of the file will open in the live editor.

  • Run the script, entering whatever input values you want to appear in the pdf
  • Under Export, select Export to pdf
  • Click Export

A pdf file containing the code and its output will be created. If you want the recipient of your pdf to see the values that were entered in response to input prompts, do not terminate the input() calls with semi-colons.

A.3 Logging Command Window Output with diary

If all you want is the command window output, independent of the code, you can create a log file with the diary command. Once logging is turned on, all command window output will be saved to a text file until it is turned off. An example of logging command window output follows:

>> diary debugging.txt
>> debugging_solution
The sum of the numbers from 1 through 1000 is 500500
The sum of the numbers from 1 through 1000 is 500500
The sum of the numbers from 1 through 1000 is 500500
The roots are -1.000000 and -2.000000
The root is 4.000000
There are 11 Fibonacci numbers that are less than 100;They are:
1
1
2
3
5
8
13
21
34
55
89
Joe's GPA is 3.07


>> test_script
Testing sums of 1 to 1000...
Testing roots of quadratics...
Testing Fibonacci Numbers...
Testing plot annotations...
Testing GPA code...
All tests passed!
>> diary off

>> type debugging.txt

debugging_solution
The sum of the numbers from 1 through 1000 is 500500
The sum of the numbers from 1 through 1000 is 500500
The sum of the numbers from 1 through 1000 is 500500
The roots are -1.000000 and -2.000000
The root is 4.000000
There are 11 Fibonacci numbers that are less than 100;They are:
1
1
2
3
5
8
13
21
34
55
89
Joe's GPA is 3.07
test_script
Testing sums of 1 to 1000...
Testing roots of quadratics...
Testing Fibonacci Numbers...
Testing plot annotations...
Testing GPA code...
All tests passed!
diary off
>>

The contents of the text file can then be merged into any report document.

A.4 Folder Sharing in MATLAB Online

In some cases, you may want others to not only see your code, but actually run and edit it. MATLAB Online has a folder-sharing capability to facilitate teamwork. To share a folder

  • Right-click on it in MATLAB Online
  • Select Share -> Invite members
  • Enter the email addresses of the users with whom you want to share the folder – they must have a Mathworks account under that email address
  • Select whether you want them to have Edit access or only View

 

The other users should now see this folder in their own MATLAB Online directory. They can now view, run, and (if you gave them edit privileges) modify the files in the folder.

License

Icon for the Creative Commons Attribution-NonCommercial 4.0 International License

MATLAB Programming for Engineering Applications Copyright © 2025 by The Ohio State University is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where otherwise noted.