How to save a variable in text file

10 views (last 30 days)
Suresh R
Suresh R on 16 Feb 2015
Edited: Stephen23 on 16 Feb 2015
Hi, I am trying to save the values in a variable to a text file. I used "-ascii" with the save command but the values in the text file are different from the one present in d variable.
temp_var=1000; save mat_to_text.txt '-ASCII'temp_var
now the contents of text file is
//mat_to_text.txt
1.0000000e+003
but the contents should be 1000.
Can somebody please help me with this! Thanks in advance :)

Accepted Answer

Stephen23
Stephen23 on 16 Feb 2015
Edited: Stephen23 on 16 Feb 2015
Actually 1.0000000e+003 is 1000.
These are just different ways of writing the same number, so you really don't need to change anything.
This way of writing numbers is called E-notation, which is a kind of scientific notation, and it is commonly used to store numbers in text data in science and engineering. One of its main advantages is the exact representation of the number of significant figures of the values. Scientific notation also lets you save numbers that are very large or very small without having to print lots of zeros: can you imagine how many characters 1.000E+300 would require if one didn't use scientific notation?
If you wish to use another number format, then you will need to use another method of writing your text file, because save does not let you control the number format: you could use fprintf for example (read the documentation for fprintf and pick the notation that best suits your needs).

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!