horzcat: Dimensions of matrices are not consistent. Error message confusion
3 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 9 Jan 2019
What should tdumpnames7DayTraj(i,2) be ?
I recommend against using importdata. I suggest using readtable() with 'HeaderLines', 12, and 'ReadVariableNames', false
Answers (1)
Neil Sheridan
on 9 Jan 2019
%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 Comments
Walter Roberson
on 10 Jan 2019
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.
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!