{"id":135,"date":"2008-04-17T09:45:58","date_gmt":"2008-04-17T14:45:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/04\/17\/processing-a-set-of-files-repost\/"},"modified":"2008-04-24T06:53:04","modified_gmt":"2008-04-24T11:53:04","slug":"processing-a-set-of-files-repost","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/04\/17\/processing-a-set-of-files-repost\/","title":{"rendered":"Processing a Set of Files &#8211; Repost"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>This is a <a href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/02\/processing-a-set-of-files\/\">repost<\/a> of an article I wrote early on in this blog's history. Since there continue to be frequent questions on the MATLAB <a href=\"\">newsgroup<\/a> regarding processing a set of files, I thought it would be worthwhile recapping this post.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Setup<\/a><\/li>\r\n         <li><a href=\"#2\">Problem Statement<\/a><\/li>\r\n         <li><a href=\"#3\">Collect the MAT-files Containing Sounds<\/a><\/li>\r\n         <li><a href=\"#4\">Check out the Files<\/a><\/li>\r\n         <li><a href=\"#6\">Loops over the Files<\/a><\/li>\r\n         <li><a href=\"#7\">What about Those Files?<\/a><\/li>\r\n         <li><a href=\"#8\">Read a File Back for Verification<\/a><\/li>\r\n         <li><a href=\"#10\">Thoughts?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Setup<a name=\"1\"><\/a><\/h3>\r\n   <p>I should start a clean workspace and with no WAV-files in my blog publishing directory.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">clear <span style=\"color: #A020F0\">all<\/span>\r\ndelete <span style=\"color: #A020F0\">*.wav<\/span><\/pre><h3>Problem Statement<a name=\"2\"><\/a><\/h3>\r\n   <p>Suppose I want to convert sounds stored in MATLAB MAT-files to files saved in WAV format for Windows.  Without explictly hardcoding\r\n      in the filenames, here's a way to proceed.\r\n   <\/p>\r\n   <h3>Collect the MAT-files Containing Sounds<a name=\"3\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">matfiles = dir(fullfile(matlabroot,<span style=\"color: #A020F0\">'toolbox'<\/span>,<span style=\"color: #A020F0\">'matlab'<\/span>,<span style=\"color: #A020F0\">'audiovideo'<\/span>,<span style=\"color: #A020F0\">'*.mat'<\/span>))<\/pre><pre style=\"font-style:oblique\">matfiles = \r\n6x1 struct array with fields:\r\n    name\r\n    date\r\n    bytes\r\n    isdir\r\n    datenum\r\n<\/pre><h3>Check out the Files<a name=\"4\"><\/a><\/h3>\r\n   <p>We can see from the length of matfiles that I have 6 MAT-files.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load(matfiles(1).name)\r\nwhos<\/pre><pre style=\"font-style:oblique\">  Name              Size             Bytes  Class     Attributes\r\n\r\n  Fs                1x1                  8  double              \r\n  matfiles          6x1               2576  struct              \r\n  y             13129x1             105032  double              \r\n\r\n<\/pre><p>Loading the data places the MAT-file contents into a structure from which I can extract the information I need and write it\r\n      back out. The sound files that MATLAB ships with store the data in <tt>y<\/tt> and the sampling frequency in <tt>Fs<\/tt>.  Let's look at the first signal.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">N = length(y);\r\nplot((1:N)\/(N*Fs),y), title(matfiles(1).name(1:end-4))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/135\/batchProcessing2008_01.png\"> <h3>Loops over the Files<a name=\"6\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> ind = 2:length(matfiles)\r\n    data = load(matfiles(ind).name);\r\n    wavwrite(data.y,data.Fs,matfiles(ind).name(1:end-4));\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Warning: Data clipped during write to file:gong\r\nWarning: Data clipped during write to file:splat\r\nWarning: Data clipped during write to file:train\r\n<\/pre><h3>What about Those Files?<a name=\"7\"><\/a><\/h3><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dir <span style=\"color: #A020F0\">*.wav<\/span><\/pre><pre style=\"font-style:oblique\">\r\ngong.wav      laughter.wav  train.wav     \r\nhandel.wav    splat.wav     \r\n\r\n<\/pre><h3>Read a File Back for Verification<a name=\"8\"><\/a><\/h3>\r\n   <p>I'll double-check the last file I just read in and wrote out.  <tt>ind<\/tt> is still set despite no longer being in the <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=46\"><tt>for<\/tt><\/a> loop. Let's check both the frequencies and the signals themselves.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[ywav, Fswav] = wavread(matfiles(ind).name(1:end-4));\r\neqFreqs = isequal(Fswav, data.Fs)\r\ndatadiff = norm(ywav-data.y)<\/pre><pre style=\"font-style:oblique\">eqFreqs =\r\n     1\r\ndatadiff =\r\n    0.0010\r\n<\/pre><p>The data stored in the WAV-file is <b>NOT<\/b> exactly the same as that stored in the MAT-file.  Reading the help for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/audiowrite.html\"><tt>wavwrite<\/tt><\/a> gives some insight; the data in the WAV-file, by default, is stored as 16-bit data vs. MATLAB's standard 64-bit double.\r\n   <\/p>\r\n   <h3>Thoughts?<a name=\"10\"><\/a><\/h3>\r\n   <p>Does it confuse people here that I don't worry about vectorization? Any other <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=135#respond\">thoughts<\/a> on this topic?\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_509d5d0e3d414d60a88d55df3a6da02e() {\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='509d5d0e3d414d60a88d55df3a6da02e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 509d5d0e3d414d60a88d55df3a6da02e';\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 = 'Loren Shure';\r\n        copyright = 'Copyright 2008 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\"><br><a href=\"javascript:grabCode_509d5d0e3d414d60a88d55df3a6da02e()\"><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.6<br><\/p>\r\n<\/div>\r\n<!--\r\n509d5d0e3d414d60a88d55df3a6da02e ##### SOURCE BEGIN #####\r\n%% Processing a Set of Files - Repost\r\n% This is a <https:\/\/blogs.mathworks.com\/loren\/2006\/08\/02\/processing-a-set-of-files\/ repost>\r\n% of an article I wrote early on in this blog's history.\r\n% Since there continue to be frequent questions on the MATLAB \r\n% < newsgroup>\r\n% regarding processing a set of files, I thought it would be worthwhile recapping\r\n% this post.  \r\n%% Setup\r\n% I should start a clean workspace and with no WAV-files in my blog\r\n% publishing directory.\r\nclear all\r\ndelete *.wav \r\n%% Problem Statement\r\n% Suppose I want to convert sounds stored in MATLAB MAT-files to files\r\n% saved in WAV format for Windows.  Without explictly hardcoding in the\r\n% filenames, here's a way to proceed.\r\n%% Collect the MAT-files Containing Sounds\r\nmatfiles = dir(fullfile(matlabroot,'toolbox','matlab','audiovideo','*.mat'))\r\n%% Check out the Files\r\n% We can see from the length of matfiles that I have 6 MAT-files.  \r\nload(matfiles(1).name)\r\nwhos\r\n%%\r\n% Loading the data places the MAT-file contents into a structure from which\r\n% I can extract the information I need and write it back out. The sound\r\n% files that MATLAB ships with store the data in |y| and the sampling\r\n% frequency in |Fs|.  Let's look at the first signal.\r\nN = length(y);\r\nplot((1:N)\/(N*Fs),y), title(matfiles(1).name(1:end-4))\r\n%% Loops over the Files\r\n% \r\nfor ind = 2:length(matfiles)\r\n    data = load(matfiles(ind).name);\r\n    wavwrite(data.y,data.Fs,matfiles(ind).name(1:end-4));\r\nend\r\n%% What about Those Files?\r\ndir *.wav\r\n%% Read a File Back for Verification\r\n% I'll double-check the last file I just read in and wrote out.  |ind| is\r\n% still set despite no longer being in the \r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=46 |for|> loop.\r\n% Let's check both the frequencies and the signals themselves.\r\n[ywav, Fswav] = wavread(matfiles(ind).name(1:end-4));\r\neqFreqs = isequal(Fswav, data.Fs)  \r\ndatadiff = norm(ywav-data.y)\r\n%%\r\n% The data stored in the WAV-file is *NOT* exactly the same as that stored\r\n% in the MAT-file.  Reading the help for \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/audiowrite.html |wavwrite|>\r\n% gives some insight; the data in the WAV-file, by default, is stored as\r\n% 16-bit data vs. MATLAB's standard 64-bit double.\r\n%% Thoughts?\r\n% Does it confuse people here that I don't worry about vectorization? Any\r\n% other <https:\/\/blogs.mathworks.com\/loren\/?p=135#respond thoughts> on this topic?  \r\n\r\n##### SOURCE END ##### 509d5d0e3d414d60a88d55df3a6da02e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      This is a repost of an article I wrote early on in this blog's history. Since there continue to be frequent questions on the MATLAB newsgroup regarding processing a set of files, I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/04\/17\/processing-a-set-of-files-repost\/\">read more >><\/a><\/p>","protected":false},"author":39,"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\/loren\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}