Why am I getting the error that the 'dimensions of arrays concatenated are not consistent'?
1 次查看(过去 30 天)
显示 更早的评论
I have a list of DOY numbers that also contain the hours and minutes that I want to convert to datetime. The variable is DOYfilt and has size 1408x1. On the 434th number in the loop, matlab gives me an error: 'Error using vertcat: Dimensions of arrays being concatenated are not consistent.'. What would be going on in the loop that would cause this when it was working beforehand?
u10_date=[];
for i=1:length(DOYfilt)
date=datestr(DOYfilt(i) + datenum('2012/01/01'));
u10_date=[u10_date;date];
end
0 个评论
回答(1 个)
Deepak
2024-8-22
Hi @Shayma Al Ali, from my understanding, you have a double array named “DOYfilt” of size 1408 x 1. You want to convert it into a datetime by adding it to the date (‘2012/01/01’) with the help of the “datestr” method in MATLAB.
You are getting an error on the 434th iteration because the values in “DOYfilt” at the 433rd and 434th indices are 50.9931 and 51.000 respectively and the dates formed at those indices are “20-Feb-2012 23:50:00” and “21-Feb-2012” respectively. Since the datatypes of these dates are different, appending them to the “u10_date” array throws an error.
To resolve this issue, we can pre-allocate the “u10_date” array as a cell array with the same size as “DOYfilt”. Then, at each index, we can save a datetime in the array. Finally, we can convert this cell array to a char array.
Here is the MATLAB code that addresses this task:
u10_date = cell(length(DOYfilt), 1); % Preallocate a cell array
for i = 1:length(DOYfilt)
date = datestr(DOYfilt(i) + datenum('2012/01/01'));
u10_date{i} = date; % Store each date string in a cell
end
u10_date_char = char(u10_date);
Attaching the documentation of methods used for reference:
I hope this resolve the issue.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!