title: How to load and store a stack of images in MATLAB
body: <p>I am trying to do my first project in MATLAB and so far I try to <strong>load a sequence of images</strong> from a directory and <strong>store them in one object</strong>.</p>
  
  <p>The images are small and they are quite few &lt; 100, so memory is not a problem.</p>
  
  <p>I would optimally store them in a <strong>3 dimensional array</strong>, but I don't know how to do it.</p>
  
  <p>Can you tell me how to load images and store all them in a stack?</p>
  
  <p>So far here is the code I have written:</p>
  
  <pre><code>function image = load_image_array(dir, start, finish)
      for i = start:finish
  
          filename = [ dir '/' sprintf('%08d', i) '.jpg' ];
          image = importdata( filename, 'jpg' );
  
          figure( i );
          imagesc( image );
  
      end
  end
  </code></pre>
  
  <p>Do you think it is a good idea to preallocate the array in advance? Also, is 3 dimensional array a good idea? I would like to have RGB images, <strong>do I need to have 3 stacks or a 4 dimensional array</strong> for this?</p>