Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Append a Column into a Text File

Subject: Append a Column into a Text File

From: Tiger

Date: 20 Sep, 2013 11:19:06

Message: 1 of 3

Hi folks,

I'm aware of the ample on-line tips on appending a new columns into an existing text file. However, my text file has the following structure:

Number Number Number Number Number Number Number Number/Number/Number

Basically they're all digits, each row has 8 columns, the first seven columns are one part i.e. one cell if I may say, and the 8th column has three numbers seperated by slashes. For instance, here's an exemplary segment of my text file:

1 1 6 12 4200 6292 0 -1/-1/-1
1 2 6 96 129 6292 0 1/-1/-1

My issue lies when appending the new column next to the 8th columns, it's complaining, but not sure why. Here's my code:

inputfilename = 'deletemenow.txt'
outputfilename = 'output.txt'
fid1 = fopen(inputfilename, 'rt')
fid2 = fopen(outputfilename, 'wt')
while feof(fid1) == 0
   tline = fgetl(fid1)
    nline = [tline, jCP, '\n']
    fprintf(fid2, nline);
end
fclose(fid1)
fclose(fid2)

...and here's the error I'm getting:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

My guts feeling tells me this is because the last column has slashes. What do you guys propose?

Ta.

Subject: Append a Column into a Text File

From: Steven_Lord

Date: 20 Sep, 2013 14:27:57

Message: 2 of 3



"Tiger " <h4ghazzawi@hotmail.co.uk> wrote in message
news:l1hava$9qv$1@newscl01ah.mathworks.com...
> Hi folks,
>
> I'm aware of the ample on-line tips on appending a new columns into an
> existing text file. However, my text file has the following structure:
>
> Number Number Number Number Number Number Number Number/Number/Number
>
> Basically they're all digits, each row has 8 columns, the first seven
> columns are one part i.e. one cell if I may say, and the 8th column has
> three numbers seperated by slashes. For instance, here's an exemplary
> segment of my text file:
>
> 1 1 6 12 4200 6292 0 -1/-1/-1
> 1 2 6 96 129 6292 0 1/-1/-1
>
> My issue lies when appending the new column next to the 8th columns, it's
> complaining, but not sure why. Here's my code:
>
> inputfilename = 'deletemenow.txt'
> outputfilename = 'output.txt'
> fid1 = fopen(inputfilename, 'rt')
> fid2 = fopen(outputfilename, 'wt')
> while feof(fid1) == 0
> tline = fgetl(fid1)
> nline = [tline, jCP, '\n']
> fprintf(fid2, nline);

This may not do quite what you want if nline can contain percent signs --
you'd probably want:

fprintf(fid2, '%s', nline)

to force FPRINTF to print the string contained in the nline variable to the
file rather than trying to interpret the contents of nline as a format
string.

> end
> fclose(fid1)
> fclose(fid2)
>
> ...and here's the error I'm getting:
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.

Because you've been FCLOSEing the files after the point where this error
occurs, if this code is in a function file you're leaving the files open. I
recommend creating onCleanup objects immediately after opening your file, so
that the files will be automatically closed regardless of whether your
function exits normally or exits due to error. See example 1 on this page:

http://www.mathworks.com/help/matlab/matlab_prog/cleaning-up-when-the-function-completes.html?searchHighlight=fclose

If this code is in a script file, you don't have this problem as per the
last section of that page -- but if you plan to convert it into a function,
keep this in mind.

> My guts feeling tells me this is because the last column has slashes. What
> do you guys propose?

Set an error breakpoint then execute your code. When MATLAB enters debug
mode due to that error breakpoint, check the sizes of the variables involved
on that line. My guess is that tline and/or jCP do not have just one row,
but have either 0 or some number greater than 1.

http://www.mathworks.com/help/matlab/debugging-code.html?searchHighlight=error

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Append a Column into a Text File

From: Tiger

Date: 30 Sep, 2013 10:44:10

Message: 3 of 3

"Steven_Lord" <slord@mathworks.com> wrote in message <l1hm1d$fmd$1@newscl01ah.mathworks.com>...
>
>
> "Tiger " <h4ghazzawi@hotmail.co.uk> wrote in message
> news:l1hava$9qv$1@newscl01ah.mathworks.com...
> > Hi folks,
> >
> > I'm aware of the ample on-line tips on appending a new columns into an
> > existing text file. However, my text file has the following structure:
> >
> > Number Number Number Number Number Number Number Number/Number/Number
> >
> > Basically they're all digits, each row has 8 columns, the first seven
> > columns are one part i.e. one cell if I may say, and the 8th column has
> > three numbers seperated by slashes. For instance, here's an exemplary
> > segment of my text file:
> >
> > 1 1 6 12 4200 6292 0 -1/-1/-1
> > 1 2 6 96 129 6292 0 1/-1/-1
> >
> > My issue lies when appending the new column next to the 8th columns, it's
> > complaining, but not sure why. Here's my code:
> >
> > inputfilename = 'deletemenow.txt'
> > outputfilename = 'output.txt'
> > fid1 = fopen(inputfilename, 'rt')
> > fid2 = fopen(outputfilename, 'wt')
> > while feof(fid1) == 0
> > tline = fgetl(fid1)
> > nline = [tline, jCP, '\n']
> > fprintf(fid2, nline);
>
> This may not do quite what you want if nline can contain percent signs --
> you'd probably want:
>
> fprintf(fid2, '%s', nline)
>
> to force FPRINTF to print the string contained in the nline variable to the
> file rather than trying to interpret the contents of nline as a format
> string.
>
> > end
> > fclose(fid1)
> > fclose(fid2)
> >
> > ...and here's the error I'm getting:
> > ??? Error using ==> horzcat
> > CAT arguments dimensions are not consistent.
>
> Because you've been FCLOSEing the files after the point where this error
> occurs, if this code is in a function file you're leaving the files open. I
> recommend creating onCleanup objects immediately after opening your file, so
> that the files will be automatically closed regardless of whether your
> function exits normally or exits due to error. See example 1 on this page:
>
> http://www.mathworks.com/help/matlab/matlab_prog/cleaning-up-when-the-function-completes.html?searchHighlight=fclose
>
> If this code is in a script file, you don't have this problem as per the
> last section of that page -- but if you plan to convert it into a function,
> keep this in mind.
>
> > My guts feeling tells me this is because the last column has slashes. What
> > do you guys propose?
>
> Set an error breakpoint then execute your code. When MATLAB enters debug
> mode due to that error breakpoint, check the sizes of the variables involved
> on that line. My guess is that tline and/or jCP do not have just one row,
> but have either 0 or some number greater than 1.
>
> http://www.mathworks.com/help/matlab/debugging-code.html?searchHighlight=error
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Hi Steve,

Thanks for your reply. I basically run a different algorithm that reads and alanyses a text file, then fclose the file. I also implement another algorithm on a different mfile (or can be in the same first algorithm's mfile) to add few columns to the original text file. I just want to be able to add columns of generated into my text file. I tried your tips but I still have pretty much the same errors.

Comments? please.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us