MATLAB Answers

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

1 view (last 30 days)
Cg Gc
Cg Gc on 9 Jan 2019
Commented: Cg Gc on 10 Jan 2019
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
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
Cg Gc
Cg Gc on 10 Jan 2019
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

Sign in to comment.

Answers (1)

Neil Sheridan
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

Cg Gc
Cg Gc on 10 Jan 2019
The problem isn't with the file names, it is what is being pulled from the files. This is why I was able to track the problem down to something in between the two dates. The code works for other years, but not for this year, making me thing that there is something wrong with the two text files, but I can't seem to find any differences.
When I tried looking at C.Data, I drew upon the 124 files it was meant to pull from. There were 12 columns with a long, very long, list of rows.
Walter Roberson
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.
Cg Gc
Cg Gc on 10 Jan 2019
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by