How do I save a workspace variable to a .txt file?

I tried using the 'save' function, but I can't seem to get it to work. I want to append a .txt file by saving a variable "excel" to it. I want to be able to do this repeatedly.
I tried the code
save(saving.txt, excel, '-append-')

2 Comments

Just to be clear. Your variable is called excel?
And I'm sure you got an error message when you run this command. Then it would be a good idea 1. to read the message, and 2. to post it here when the solution is not clear after 1.

Sign in to comment.

 Accepted Answer

save('myFile.txt', 'excel', '-ASCII','-append');

6 Comments

That worked well. How about text? This is the code I put in
save('AppendedData.txt', 'diename', '-ASCII', '-append');
save('AppendedData.txt', 'excel', '-ASCII', '-append');
Here's the output I got that ended up in the .txt file
9.7000000e+001 1.1500000e+002 1.0000000e+002 1.0200000e+002 5.9000000e+001 1.0800000e+002 1.0600000e+002 1.0700000e+002 9.7000000e+001 1.1500000e+002 1.0000000e+002 1.0200000e+002
Hi,
I am using the same code but I would like to have values without the 10power: 9.7 1.15 -------- I am struggling to fixe it. Can some one help me?
Has anyone figured out how to remove the 10power?
Hello, what is '-append' stand for ?
I tried without '-append', also worked.
Thank you!
Those are not 9.7 and 1.15 . Those are 97 and 115 which are the character positions of the characters you tried to save. save with -ascii is reserved for numeric values and cannot be used to save text. If you need to save text you will need to fopen/fprintf/fclose . Or there is a hack with dlmwrite. Or Windows with excel installed can xlswrite a cell array containing character vectors. or writetable can write csv files from tables that include character vectors .
Without the -append every save will erase all existing content of the file.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!