Trying to input and modify a .txt file, getting Matrix Dimension not consistent error when outputting new .txt file

2 views (last 30 days)
Hello! I am trying to modify a file that has a list of dates, in which the day is listed as the day of the year, with no month entry (Ex: February 15th is '046'). The program will convert it to the traditional format, by running the day though a series of if statements and changing the value accordingly for each month.
The error I am getting is this:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
I am aware of why one cannot concatenate matrices of differing sizes, but that is not what I am trying to do, and I don't see why the code is trying to do so. What I WANT is to take in a space delimited file as a matrix, and then create a NEW matrix in which one of the columns (a day 1-365) has been split into two columns (a Day 0-31, and a month 0-12).
Now, this program is based on a very similar one that works fine (It had no If statements, but rather took in 3 of the columns, computed some operations on them, and entered them all into a new column that replaced the 3 in a new file). I simply took the code of that one and replaced the operation i just mentioned with the IF statements you see below.
I take in:
.txt file, space delimited, formatted as such:
2011 058 13 27 45.29
2011 059 17 18 52.23
2011 061 18 43 01.62
And I want to get out this:
2011 2 1 13 27 45.29
2011 2 2 17 18 52.23
2011 2 4 18 43 01.62
The input I have above gets read in with this code:
DatesFile = '/directory/in.txt';
%READ input (Year, Day of Year, Hour, Minute, Second)
[Yr,DoY,Hour,Min,Sec] = textread(DatesFile,'%n%n%n%n%n','delimiter',' ');
It then gets converted with:
% convert day of year to day of month format. (2007, not leap)
JanDays=31;
FebDays=28;
MarDays=31;
AprDays=30;
MayDays=31;
JunDays=30;
JulDays=31;
AugDays=31;
SepDays=30;
OctDays=31;
NovDays=30;
DecDays=31;
Mon=0;
DoM=0;
%JAN
if(DoY<=JanDays)
Mon = 1; %SET month
DoM = DoY; %SET day of month
%FEB
elseif(DoY<=JanDays+FebDays)
Mon = 2;
DoM = Doy - JanDays;
%MAR
elseif(DoY<=JanDays+FebDays+MarDays)
Mon = 3;
DoM = Doy - JanDays-FebDays;
%APR
elseif(DoY<=JanDays+FebDays+MarDays+AprDays)
Mon = 4;
DoM = Doy - JanDays-FebDays-MarDays;
%MAY
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays)
Mon = 5;
DoM = Doy - JanDays-FebDays-MarDays-AprDays;
%JUN
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays)
Mon = 6;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays;
%JUL
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays+JulDays)
Mon = 7;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays-JunDays;
%AUG
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays+JulDays+AugDays)
Mon = 8;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays-JunDays-JulDays;
%SEP
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays+JulDays+AugDays+SepDays)
Mon = 9;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays-JunDays-JulDays-AugDays;
%OCT
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays+JulDays+AugDays+SepDays+OctDays)
Mon = 10;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays-JunDays-JulDays-AugDays-SeptDays;
%NOV
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays+JulDays+AugDays+SepDays+OctDays+NovDays)
Mon = 11;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays-JunDays-JulDays-AugDays-SepDays-OctDays;
%DEC
elseif(DoY<=JanDays+FebDays+MarDays+AprDays+MayDays+JunDays+JulDays+AugDays+SepDays+OctDays+NovDays)
Mon = 12;
DoM = Doy - JanDays-FebDays-MarDays-AprDays-MayDays-JunDays-JulDays-AugDays-SepDays-OctDays-NovDays;
end
LAST, it outputs the file. HERE is where I am getting my error:
% Define 6 Columns as a matrix
Q = [Yr,Mon,DoM,Hour,Min,Sec];
%OUTPUT text file with julian dates
dlmwrite('/u/castro/Desktop/Out.txt',Q,'delimiter',' ','precision','%.2f')
So while I do know what the cause of my error is... I don't see why it wants to concatenate these two matrices. I have done this in the past where I took in a matrix of one size and spat out a matrix of another size and it worked fine.
I would appreciate an answer that fixes this code to work, but if you have a much better way of approaching the problem, feel free to suggest that as well.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Feb 2013
Edited: Walter Roberson on 16 Feb 2013
DoY is a vector, but you are treating it as a scalar, so you are producing a single Mon value for all of your dates; the code then bombs when you try to put together that one Mon value with your vectors.
Have you considered using datevec() instead of the calculation you are using now?
  5 Comments
Brian Castro
Brian Castro on 18 Feb 2013
Edited: Brian Castro on 18 Feb 2013
And one last thing. I am writing out with
dlmwrite('/u/castro/Desktop/Out2.txt',Q,'delimiter',' ','precision','%g').
I want leading zeroes (01, 02... etc) on the columns for Day and Month. How do I do this? I tried using dec2bin(1,2), which was suggested in another thread (By you... actually) but it does not seem to work in this context.
Thanks!
Walter Roberson
Walter Roberson on 18 Feb 2013
Try precision '%02g'
The first line makes use of a bit of a trick. If you construct a date vector and exceed the normal maximum for a field, the date calculation routines move as much extra as necessary up to the next larger field(s). So you can construct the 118'th day of 1975 as (text equivalent) January 118 1975, and the date routines know to convert that to April 28 1975. So the [Years ones Days_of_Years] is doing that construction, January {day of year} 1975; the datenum() converts that to serial date number, just to pass it through some date routine, and datevec() around that turns it back to a date vector with corrected fields.
Years(:) is MATLAB notation for converting the vector (or array) named Years into a single column vector. Likewise for Days_of_Years, make it a column vector. And then the ones() bits constructs a column vector of "1" corresponding to inserting the constant month "January" (month #1) for all those entries. That is, the code I gave is vectorized, above to handle vectors of years and day-of-year, and handle them whether you stored them in row or column form.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export 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!