Pulling my hair out over this simple for loop! Please help!
1 次查看(过去 30 天)
显示 更早的评论
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!
2 个评论
Ahmet Cecen
2016-3-7
编辑:Ahmet Cecen
2016-3-7
Cannot replicate the error with the pieces of codes you have given. Unless you share the entire code with the data, the best way to resolve a situation like this is to use dbstop. Go to your command line and write:
dbstop if error
Then run your entire script using the play shaped run button in the editor or writing the name of the script in the command line and pressing enter.
The program will stop running at the exact moment you error is happening, with the values of every variable at the moment of the error. Hopefully you can figure out why the error is happening using that information. If you only have scripts and no functions this might not be of much help though.
采纳的回答
Walter Roberson
2016-3-7
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.
0 个评论
更多回答(1 个)
Ahmet Cecen
2016-3-7
编辑:Ahmet Cecen
2016-3-7
Its the brackets. You cannot use normal brackets for vectors of strings as they are stored as cell arrays.
for i = 1:length(ia1)
trade{i}=date(ia1(i):ia2(i));
end
Actually that is not the only thing, your vector size of dates for each i is different as well, so it wouldn't have worked either way.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!