MATLAB: Pulling the hair out over this simple for loop! Please help!

datetimefor loopMATLAB

I keep getting the "subscripted assignment dimension mismatch" error. My variables are:
ia1=[79;91;94]
ia2=[80;103;95]
date = vector of datetimes (right below)
'08/25/2008'
'08/26/2008'
'08/27/2008'
'08/28/2008'
'08/29/2008'
'09/02/2008'
'09/03/2008'
'09/04/2008'
'09/05/2008'
'09/08/2008'
'09/09/2008'
'09/10/2008'
'09/11/2008'
'09/12/2008'
'09/15/2008'
'09/16/2008'
'09/17/2008'
'09/18/2008'
'09/19/2008'
'09/22/2008'
'09/23/2008'
'09/24/2008'
'09/25/2008'
'09/26/2008'
'09/29/2008'
'09/30/2008'
'10/01/2008'
'10/02/2008'
'10/03/2008'
'10/06/2008'
'10/07/2008'
'10/08/2008'
'10/09/2008'
'10/10/2008'
'10/13/2008'
'10/14/2008'
'10/15/2008'
'10/16/2008'
'10/17/2008'
'10/20/2008'
'10/21/2008'
'10/22/2008'
'10/23/2008'
'10/24/2008'
'10/27/2008'
'10/28/2008'
'10/29/2008'
'10/30/2008'
'10/31/2008'
'11/03/2008'
'11/04/2008'
'11/05/2008'
'11/06/2008'
'11/07/2008'
'11/10/2008'
'11/11/2008'
'11/12/2008'
'11/13/2008'
'11/14/2008'
'11/17/2008'
'11/18/2008'
'11/19/2008'
'11/20/2008'
'11/21/2008'
'11/24/2008'
'11/25/2008'
'11/26/2008'
'11/28/2008'
'12/01/2008'
'12/02/2008'
'12/03/2008'
'12/04/2008'
'12/05/2008'
'12/08/2008'
'12/09/2008'
'12/10/2008'
'12/11/2008'
'12/12/2008'
'12/15/2008'
'12/16/2008'
'12/17/2008'
'12/18/2008'
'12/19/2008'
'12/22/2008'
'12/23/2008'
'12/24/2008'
'12/26/2008'
'12/29/2008'
'12/30/2008'
'12/31/2008'
'01/02/2009'
'01/05/2009'
'01/06/2009'
'01/07/2009'
'01/08/2009'
'01/09/2009'
'01/12/2009'
'01/13/2009'
'01/14/2009'
'01/15/2009'
'01/16/2009'
'01/20/2009'
'01/21/2009'
'01/22/2009'
'01/23/2009'
Here is my loop:
for i = 1:length(ia1)
trade(i)=date(ia1(i):ia2(i));
end
If I change the second line to
trade=date(ia1(i):ia2(i));
then the loop works, but it overrides the first two vectors and leaves just the last one.
I am extremely grateful for any help!

Best Answer

You need to initialize trade as cell, such as
trade = {};
Then in your loop you need
trade{i} = date(ia1(i):ia2(i));
You are extracting a number of datetime objects at the same time, so each date(ia1(i):ia2(i)) is going to produce a vector of datetime objects. You have been attempting to store that vector into a single location, trade(i) . Only cell arrays can store multiple values inside locations designated with a single index.
Later you will need to use {} notation to extract the vector of datetime objects when you want to use the vector.