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
2019년 1월 9일
What should tdumpnames7DayTraj(i,2) be ?
I recommend against using importdata. I suggest using readtable() with 'HeaderLines', 12, and 'ReadVariableNames', false
답변 (1개)
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!