Thread Subject: Remove the last line from a .csv file.

Subject: Remove the last line from a .csv file.

From: Mark Hard

Date: 15 Jun, 2011 13:34:21

Message: 1 of 5

Hi guys,

Basically what I want to do is write an array I have to a .csv file and then add a string to the top of the array. I have done this successfully using the following code.

csvwrite('data.csv',data);

dlmwrite('data.csv',['header string' 13 10 fileread('data.csv')],'delimiter','');

However I've noticed that each of these lines adds a blank line to the end of the file. When I open the .csv file in my other analysis software I get an error and this is only solved by removing one of the blank lines from the end.

Any reccomendations would be greatly appreciated!

Thanks,

Mark

Subject: Remove the last line from a .csv file.

From: dpb

Date: 15 Jun, 2011 14:48:53

Message: 2 of 5

Mark Hard wrote:

> Hi guys,
>
> Basically what I want to do is write an array I have to a .csv file and
> then add a string to the top of the array. I have done this successfully
> using the following code.
>
> csvwrite('data.csv',data);
>
> dlmwrite('data.csv',['header string' 13 10
> fileread('data.csv')],'delimiter','');
>
> However I've noticed that each of these lines adds a blank line to the
> end of the file. When I open the .csv file in my other analysis software
> I get an error and this is only solved by removing one of the blank
> lines from the end.
>
> Any reccomendations would be greatly appreciated!


Why in the world are you writing the data first, then turning around and
overwriting it after reading it???????

Write the header, then then the data and be done.

I'd think that would solve the problem...

--

Subject: Remove the last line from a .csv file.

From: TideMan

Date: 16 Jun, 2011 05:01:35

Message: 3 of 5

On Jun 16, 1:34 am, "Mark Hard" <GerryTheLe...@hotmail.com> wrote:
> Hi guys,
>
> Basically what I want to do is write an array I have to a .csv file and then add a string to the top of the array. I have done this successfully using the following code.
>
> csvwrite('data.csv',data);
>
> dlmwrite('data.csv',['header string' 13 10 fileread('data.csv')],'delimiter','');
>
> However I've noticed that each of these lines adds a blank line to the end of the file. When I open the .csv file in my other analysis software I get an error and this is only solved by removing one of the blank lines from the end.
>
> Any reccomendations would be greatly appreciated!
>
> Thanks,
>
> Mark

Oh what a muddle you've gotten yourself into using these functions.

If you use fprintf instead, it's much simpler:
[nrows,ncols]=size(data);
fid=fopen('data.csv','wt');
fprintf(fid,'%s\n',Header); % Assumes Header is a string
fmt=repmat('%0.3f,',1,ncols); % Set up format
fmt=[fmt(1:end-1) '\n']; % Remove comma and put LF at the end
fprintf(fid,fmt,data'); % Need to transpose mx
fclose(fid);

Subject: Remove the last line from a .csv file.

From: Mark Hard

Date: 16 Jun, 2011 10:34:05

Message: 4 of 5

Thanks very much guys. I've seen the error of my ways!

Subject: Remove the last line from a .csv file.

From: Rune Allnor

Date: 16 Jun, 2011 10:47:25

Message: 5 of 5

On Jun 16, 12:34 pm, "Mark Hard" <GerryTheLe...@hotmail.com> wrote:
> Thanks very much guys. I've seen the error of my ways!

Just to be clear: A file is not *Random* Access Memory (RAM),
but *Sequential* Access Memory. With variables in RAM you can
access this or that part at will; with data in a file you need
to access them in the order they appear in the file.

Which means that you need to write the data to your file in
the order they are supposed to appear in the file. So if you
want a header and then data, then you need to write the header
first, before you write the data.

Rune

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com