title: MATLAB: Load files from folder by extension
body: <p>What is the easiest way to load all files from a folder with the same extension into MATLAB?</p>
  
  <p>Previous solutions by me:</p>
  
  <pre><code>%%% Will load a file if its filename is provided
  %%% USAGE: (Best save data to a variable to work with it.)
  %%% &gt;&gt; x = loadwrapper('&lt;file_name&gt;')
  %%% ... and then use 'x' all the way you want.
  %%% &lt;file_name&gt; works with absolute and relative paths, too.
  
  function [ loaded_data ] = loadwrapper( file_name )
  
      files = dir(file_name);
      loaded_data = load(files.name);
  
  end
  </code></pre>
  
  <p>and </p>
  
  <pre><code>%%% put this in a new script, in a function it WILL NOT WORK!
  %%% and fix your paths, ofc. i left mine in here on purpose.
  
  
  %%% SETTINGS
  folderName='/home/user/folder/';
  extension='*.dat';
  
  
  %%% CODE
  concattedString=strcat(folderName, extension);
  fileSet=dir(concattedString); 
  
  % loop from 1 through to the amount of rows
  for i = 1:length(fileSet)
  
      % load file with absolute path, 
      % the fileSet provides just the single filename
      load (strcat(folderName, fileSet(i).name)); 
  
  end
  
  
  %%% TIDY UP
  %%% only imported files shall stay in workspace area
  clear folderName;
  clear extension;
  clear concattedString;
  clear fileSet;
  clear i;
  </code></pre>