{"id":3056,"date":"2011-12-30T06:37:14","date_gmt":"2011-12-30T11:37:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3056"},"modified":"2017-02-04T14:30:43","modified_gmt":"2017-02-04T19:30:43","slug":"automatically-add-copyright-information-to-your-matlab-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/12\/30\/automatically-add-copyright-information-to-your-matlab-files\/","title":{"rendered":"Automatically add copyright information to your MATLAB files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/16441-addcopyright\"><tt>addcopyright<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/17512\">Mirko Hrovat<\/a>.\r\n   <\/p>\r\n   <p>As a customer-facing engineer at MathWorks, I distribute a lot of MATLAB example files, including files posted on the File\r\n      Exchange. As a policy, we add copyright information to our MATLAB files. I spend some non-trivial amount of time opening files\r\n      and adding the copyright information to all of my files just before distribution. Little did I know that Mirko had created\r\n      a nice utility for helping me with the process! He took the original entry by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/1608\">Raymond<\/a>, one of my colleagues, and made some improvements to it.\r\n   <\/p>\r\n   <p>The function adds a copyright text in the following format:<\/p><pre> %   Copyright 2011 The MathWorks, Inc.<p><\/p><\/pre><p>One of lesser known features of a copyright string like the one above is that when you <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_env\/f6-22451.html\">publish<\/a> a file with this comment, it adds a footnote at the bottom with the copyright information:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_addcopyright\/copyright_footnote.png\"> <\/p>\r\n   <p>The function is easy to use. To add a copyright line to all MATLAB files under \"C:myDemos\", simply type:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">addcopyright <span style=\"color: #A020F0\">C:myDemos<\/span><\/pre><p>or you can apply this to individual files:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">addcopyright <span style=\"color: #A020F0\">myFcn.m<\/span><\/pre><p>The function has many other options, such as customizing the organization name and the copyright year. Mirko describes in\r\n      detail the modifications to Raymond's entry, but here are some of the ones I like:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><i><b>Copyright line is added either before $Revision keyword or after a blank line.<\/b><\/i> MATLAB files shipped with the product all contain revision information (edit any MATLAB function). But most of my files don't\r\n            have a \"$Revision\" line, so I like the fact that it looks for a blank line.\r\n         <\/li>\r\n         <li><i><b>Ability to have multiple copyrights with different organizations.<\/b><\/i> If the file already contains a copyright, calling the function with a different organization name appends a new copyright\r\n            line. This is useful when I am updating a file written by someone else from a different organization.\r\n         <\/li>\r\n         <li><i><b>Automatic update of multi-year copyrights.<\/b><\/i> If the file already contains \"Copyright 2009\", then calling it again with 2011 will change it to \"Copyright 2009-2011\".\r\n         <\/li>\r\n         <li><i><b>Support for command syntax.<\/b><\/i> <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/syntax.html#f76-1098266\">Command syntax<\/a> is useful when appropriate, and I think it's quite appropriate here. My examples above use command syntax. Compare this with\r\n            the function syntax: <tt>addcopyright('myFcn.m')<\/tt><\/li>\r\n         <li><i><b>Ability to remove copyright line.<\/b><\/i> It's always nice to be able to undo the process.\r\n         <\/li>\r\n         <li><i><b>Ability to add copyright to a single file (as opposed to all files inside folders).<\/b><\/i> Sometimes, I just want to update a single file within a folder, so this is quite useful for me.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Thanks Mirko for this useful entry!<\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3056#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/16441-addcopyright#comments\">comment<\/a> for Mirko.\r\n   <\/p>\r\n   <p>Oh, and I wish everyone a great new upcoming year!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_3f28cad08a7d4a7c8c0d69a9c19f4988() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='3f28cad08a7d4a7c8c0d69a9c19f4988 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3f28cad08a7d4a7c8c0d69a9c19f4988';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use 'x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/x3C\/g, 'x26#60;');\r\n\r\n        author = 'Jiro Doke';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\">Copyright 2011 The MathWorks, Inc.<br><a href=\"javascript:grabCode_3f28cad08a7d4a7c8c0d69a9c19f4988()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.13<br><\/p>\r\n<\/div>\r\n<!--\r\n3f28cad08a7d4a7c8c0d69a9c19f4988 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/16441-addcopyright\r\n% |addcopyright|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/17512 Mirko\r\n% Hrovat>.\r\n%\r\n% As a customer-facing engineer at MathWorks, I distribute a lot of MATLAB\r\n% example files, including files posted on the File Exchange. As a policy,\r\n% we add copyright information to our MATLAB files. I spend some\r\n% non-trivial amount of time opening files and adding the copyright\r\n% information to all of my files just before distribution. Little did I\r\n% know that Mirko had created a nice utility for helping me with the\r\n% process! He took the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/3949 original entry>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/1608\r\n% Raymond>, one of my colleagues, and made some improvements to it.\r\n%\r\n% The function adds a copyright text in the following format:\r\n%\r\n%   %   Copyright 2011 The MathWorks, Inc.\r\n%\r\n% One of lesser known features of a copyright string like the one above is\r\n% that when you\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_env\/f6-22451.html publish>\r\n% a file with this comment, it adds a footnote at the bottom with the\r\n% copyright information:\r\n%\r\n% <<copyright_footnote.png>>\r\n%\r\n% The function is easy to use. To add a copyright line to all MATLAB files\r\n% under \"C:myDemos\", simply type:\r\n\r\naddcopyright C:myDemos\r\n\r\n%%\r\n% or you can apply this to individual files:\r\n\r\naddcopyright myFcn.m\r\n\r\n%%\r\n% The function has many other options, such as customizing the organization\r\n% name and the copyright year. Mirko describes in detail the modifications\r\n% to Raymond's entry, but here are some of the ones I like:\r\n%\r\n% * _*Copyright line is added either before $Revision keyword or after a\r\n% blank line.*_ MATLAB files shipped with the product all contain revision\r\n% information (edit any MATLAB function). But most of my files don't have a\r\n% \"$Revision\" line, so I like the fact that it looks for a blank line.\r\n% * _*Ability to have multiple copyrights with different organizations.*_\r\n% If the file already contains a copyright, calling the function with a\r\n% different organization name appends a new copyright line. This is useful\r\n% when I am updating a file written by someone else from a different\r\n% organization.\r\n% * _*Automatic update of multi-year copyrights.*_ If the file already\r\n% contains \"Copyright 2009\", then calling it again with 2011 will change it\r\n% to \"Copyright 2009-2011\".\r\n% * _*Support for command syntax.*_\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/syntax.html#f76-1098266\r\n% Command syntax> is useful when appropriate, and I think it's quite\r\n% appropriate here. My examples above use command syntax. Compare this with\r\n% the function syntax: |addcopyright('myFcn.m')|\r\n% * _*Ability to remove copyright line.*_ It's always nice to be able to\r\n% undo the process.\r\n% * _*Ability to add copyright to a single file (as opposed to all files\r\n% inside folders).*_ Sometimes, I just want to update a single file within\r\n% a folder, so this is quite useful for me.\r\n%\r\n% Thanks Mirko for this useful entry!\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=3056#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/16441-addcopyright#comments\r\n% comment> for Mirko.\r\n%\r\n% Oh, and I wish everyone a great new upcoming year!\r\n\r\n%   Copyright 2011 The MathWorks, Inc.\r\n##### SOURCE END ##### 3f28cad08a7d4a7c8c0d69a9c19f4988\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is addcopyright by Mirko Hrovat.\r\n   \r\n   As a customer-facing engineer at MathWorks, I distribute a lot of MATLAB example files, including files posted on the File\r\n     ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/12\/30\/automatically-add-copyright-information-to-your-matlab-files\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3056"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3056"}],"version-history":[{"count":26,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3056\/revisions"}],"predecessor-version":[{"id":8385,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3056\/revisions\/8385"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}