File Exchange Pick of the Week

Our best user submissions

Automatically add copyright information to your MATLAB files

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.

Published with MATLAB® 7.13

  • print
  • send email


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.