File Exchange Pick of the Week

Our best user submissions

Automatically add copyright information to your MATLAB files 3

Posted by Jiro Doke,

Jiro's pick this week is addcopyright by Mirko Hrovat.

As a customer-facing engineer at MathWorks, I distribute a lot of MATLAB example files, including files posted on the File Exchange. As a policy, we add copyright information to our MATLAB files. I spend some non-trivial amount of time opening files and adding the copyright information to all of my files just before distribution. Little did I know that Mirko had created a nice utility for helping me with the process! He took the original entry by Raymond, one of my colleagues, and made some improvements to it.

The function adds a copyright text in the following format:

 %   Copyright 2011 The MathWorks, Inc.

One of lesser known features of a copyright string like the one above is that when you publish a file with this comment, it adds a footnote at the bottom with the copyright information:

The function is easy to use. To add a copyright line to all MATLAB files under "C:myDemos", simply type:

addcopyright C:myDemos

or you can apply this to individual files:

addcopyright myFcn.m

The function has many other options, such as customizing the organization name and the copyright year. Mirko describes in detail the modifications to Raymond's entry, but here are some of the ones I like:

  • Copyright line is added either before $Revision keyword or after a blank line. MATLAB files shipped with the product all contain revision information (edit any MATLAB function). But most of my files don't have a "$Revision" line, so I like the fact that it looks for a blank line.
  • Ability to have multiple copyrights with different organizations. If the file already contains a copyright, calling the function with a different organization name appends a new copyright line. This is useful when I am updating a file written by someone else from a different organization.
  • Automatic update of multi-year copyrights. If the file already contains "Copyright 2009", then calling it again with 2011 will change it to "Copyright 2009-2011".
  • Support for command syntax. Command syntax is useful when appropriate, and I think it's quite appropriate here. My examples above use command syntax. Compare this with the function syntax: addcopyright('myFcn.m')
  • Ability to remove copyright line. It's always nice to be able to undo the process.
  • Ability to add copyright to a single file (as opposed to all files inside folders). Sometimes, I just want to update a single file within a folder, so this is quite useful for me.

Thanks Mirko for this useful entry!


Give this a try and let us know what you think here or leave a comment for Mirko.

Oh, and I wish everyone a great new upcoming year!

Copyright 2011 The MathWorks, Inc.
Get the MATLAB code

Published with MATLAB® 7.13


Comments are closed.

3 CommentsOldest to Newest

Sam Roberts replied on : 1 of 3

Hey Jiro,

Sam Roberts here – hope you remember me from MathWorks.

Nice post – and a nice tip about how the publish command will convert a copyright line into a nicely formatted footnote in the HTML.

Unfortunately, one of the things I’ve discovered since moving on from MathWorks is that this tip only works if you are at MathWorks! I had no idea until I tried publishing a script with another company’s title in the copyright line, but it’s true. It turns out that the string “The MathWorks, Inc.” is actually hardcoded into the publishing functionality!

PS My New Year’s resolution is to write my MATLAB blog much more frequently, so check back!

Happy New Year,


Jiro Doke replied on : 2 of 3

Hi Sam! It’s great to hear from you! Hope you’re doing well.

Oh wow, I didn’t know about that. Thanks for pointing that out. I’ll check on that enhancement.

And thanks for the reference to your blog. I just added your site to my RSS feed. Looking forward to your frequent posts. :)

Happy New Year!

Jeff K replied on : 3 of 3

Hi Jiro,

I concur with Sam Roberts that support for user-definable company strings (e.g. Acme, Inc.) would be great. The footer is a more agreeable location for such legalese than inline with the help text itself.

The directions he provided for editing the m2mxdom.m file work, but I am not in the habit of propagating edits to internal MATLAB files across our developer pool.