adding data to a text file

i have a file called hello.txt using wordpad which contains the matrix
2 7 3
2 6 9
now i have a vector v = [1 2 3] and i want to add this vector to the hello.txt file so when i open the hello.txt file i should have
2 7 3
2 6 9
1 2 3
how can i do this

1 comentario

Walter Roberson
Walter Roberson el 15 de Mzo. de 2012
Duplicate is at http://www.mathworks.com/matlabcentral/answers/32377-appending-a-file

Iniciar sesión para comentar.

 Respuesta aceptada

Wayne King
Wayne King el 15 de Mzo. de 2012

2 votos

You can use:
dlmwrite('FILENAME',M,'-append')
For example:
x = [2 7 3; 2 6 9];
dlmwrite('test.dat',x);
y = [1 2 3];
dlmwrite('test.dat',y,'-append');

4 comentarios

lowcalorie
lowcalorie el 15 de Mzo. de 2012
i tried that and dlmwrite just keeps overwriting my file
Wayne King
Wayne King el 15 de Mzo. de 2012
and you are using '-append'??? Just copy and paste my example, it works.
lowcalorie
lowcalorie el 15 de Mzo. de 2012
if i already have a file what fopen should i be using
Walter Roberson
Walter Roberson el 15 de Mzo. de 2012
Wayne's code is designed for the case where the file is _not_ open.
The code that opens the file is shown in your duplicate question, referenced in the above comments.

Iniciar sesión para comentar.

Más respuestas (1)

Geoff
Geoff el 15 de Mzo. de 2012

1 voto

I remember answering an almost identical question from you the other day. It used the fopen() with the open-mode 'w' to overwrite.
To append, use the open-mode 'a'.

2 comentarios

lowcalorie
lowcalorie el 15 de Mzo. de 2012
thats what im doing but it keeps overwriting there muct be something wrong with other parts of my script then
thanks
Walter Roberson
Walter Roberson el 15 de Mzo. de 2012
"lowcalorie" is using WordPad to examine the file. "at" is needed instead of just "a", as WordPad is an old editor that only understands lines if they end in the combination CARRIAGE-RETURN LINEFEED which you do not get if you use only "a" permissions when you are using MS Windows.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Mzo. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by