File Exchange Pick of the Week

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!

Comments

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

3 CommentsOldest to Newest

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!

I think there’s an enhancement in already to change that, but in the meantime your readers might like to read a blog post I wrote that details how you can make some simple edits to the publishing functionality that will make it work for your company too:

http://www.xmwconsulting.co.uk/backslashblog/2011/10/07/adding-copyright-to-published-scripts/

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

Happy New Year,

Sam

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!

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.

-jeff

These postings are the author's and don't necessarily represent the opinions of MathWorks.