Trying to input and modify a .txt file, getting Matrix Dimension not consistent error when outputting new .txt file
Show older comments
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
More Answers (0)
Categories
Find more on Data Type Conversion 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!