horzcat: Dimensions of matrices are not consistent. Error message confusion

1 次查看(过去 30 天)
I am running a very long and not well scripted code to plot trajectories a month at a time. I also also doing some pretty off the wall math to do different calculations.
My code works great for the entire year, until I get to Christmas, then I get the horzcat error messge.
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in Plot_7day_traj_Fullyear (line 1186)
t7Data = [t7Data C.data(:,9:12)];
I tracked the error to a problem between two files, but the files have the same number of rows and columns as they were generated in sequence by the same program at the same time.
Here is the entire code. The short version of it. I excluded months January-November as they work perfectly fine. Attached are the troublesome files. tdump86122418 is brought into t7Data. tdump86122500 is not. I would attach more, but there are 124 for December.
%December
delimiterIn = ' ';
headerlinesIn = 12;%12 in this year
t7Data = [];
p7Data = [];
for i = 1337:1460 %Set this to the number of files you want to import.
C = importdata(tdumpnames7DayTraj(i,2),delimiterIn,headerlinesIn);
t7Data = [t7Data C.data(:,9:12)];
p7Data = [p7Data C.data(:,9:13)];
end
I don't mean to be a pain, but if someone can find a difference between the two text files and explain why I am getting this message, I would appreciate it. Another odd quirk, if I change (:,9:12) to (:,anynumberlessthan9:12) it works fine. I thought about doing this, but it throws the scripts I have following it all out of wack.
As far as I can tell, it just appears for an unknown reason and then disappears. Not
  2 个评论
Walter Roberson
Walter Roberson 2019-1-9
What should tdumpnames7DayTraj(i,2) be ?
I recommend against using importdata. I suggest using readtable() with 'HeaderLines', 12, and 'ReadVariableNames', false
Cg Gc
Cg Gc 2019-1-10
tdumpnames7DayTraj is a string that has several columns of names for files where I am pulling the trajectory information from.
Capture7.JPG
The names are similar to the examples I attached, but they are not text files. They are extensionless files. There are 124 for months with 31 days in them. 1460 to 1484 files for each year.
readtable doesn't read the files because they are extensionless.
Capture8.JPG

请先登录,再进行评论。

回答(1 个)

Neil Sheridan
Neil Sheridan 2019-1-9
%December
delimiterIn = ' ';
headerlinesIn = 12;%12 in this year
t7Data = [];
p7Data = [];
tdumpnames7DayTraj = {'tdump86122418.txt','tdump86122500.txt'};
for i = 1:2 %Set this to the number of files you want to import.
C = importdata(tdumpnames7DayTraj{i},delimiterIn,headerlinesIn);
t7Data = [t7Data C.data(:,9:12)];
p7Data = [p7Data C.data(:,9:13)];
end
I tried with the above code but couldn't reproduce error. Could you set "Stop on error" under Breakpoints and examine the C.data?
  3 个评论
Walter Roberson
Walter Roberson 2019-1-10
Well that's your problem them: the files you provided to us have 22 columns, and that is going to be incompatible with the files that only have 12 columns.
Cg Gc
Cg Gc 2019-1-10
All files have the same number of columns. I am only needing columns 9-12 or 9-13.
There is something that MATLAB doesn't like with the one Christmas trajectory. I skipped over it and everything works fine now.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by