{"id":48,"date":"2006-08-02T09:52:04","date_gmt":"2006-08-02T14:52:04","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=48"},"modified":"2006-08-08T12:15:21","modified_gmt":"2006-08-08T17:15:21","slug":"processing-a-set-of-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/02\/processing-a-set-of-files\/","title":{"rendered":"Processing a Set of Files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There continue to be questions on the MATLAB <a href=\"\">newsgroup<\/a> regarding processing a set of files.  So, for the record, and even though Steve covered this topic in <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=61\">his blog<\/a>, I thought I'd get an answer on record here as well.\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\n7x1 struct array with fields:\r\n    name\r\n    date\r\n    bytes\r\n    isdir\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 7 MAT-files.  I know I don't care about the first one in this case so I\r\n      am starting my analysis with the second one.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load(matfiles(2).name)\r\nwhos<\/pre><pre style=\"font-style:oblique\">  Name           Size                    Bytes  Class\r\n\r\n  Fs             1x1                         8  double array\r\n  matfiles       7x1                      2435  struct array\r\n  y          13129x1                    105032  double array\r\n\r\nGrand total is 13390 elements using 107475 bytes\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(2).name(1:end-4))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/48\/batchProcessing_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\nchirp.wav     handel.wav    splat.wav     \r\ngong.wav      laughter.wav  train.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=\"?p=48#respond\">thoughts<\/a> on this topic?\r\n   <\/p>\r\n   <p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n##### SOURCE BEGIN #####\r\n%% Processing a Set of Files\r\n% There continue to be questions on the MATLAB \r\n% < newsgroup>\r\n% regarding processing a set of files.  So, for the record, and even\r\n% though Steve covered this topic in\r\n% <https:\/\/blogs.mathworks.com\/steve\/?p=61 his blog>, I thought I'd get an\r\n% answer on record here as well.\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 7 MAT-files.  I know I\r\n% don't care about the first one in this case so I am starting my analysis\r\n% with the second one.\r\nload(matfiles(2).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(2).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 <?p=48#respond thoughts> on this topic?  \r\n\r\n##### SOURCE END #####\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There continue to be questions on the MATLAB newsgroup regarding processing a set of files.  So, for the record, and even though Steve covered this topic in his blog, I thought I'd get... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/08\/02\/processing-a-set-of-files\/\">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\/48"}],"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=48"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}