adding data to a text file
95 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2012-3-15
Duplicate is at http://www.mathworks.com/matlabcentral/answers/32377-appending-a-file
采纳的回答
Wayne King
2012-3-15
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 个评论
Walter Roberson
2012-3-15
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.
更多回答(1 个)
Geoff
2012-3-15
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 个评论
Walter Roberson
2012-3-15
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!