saving matrices to text files
2 views (last 30 days)
Show older comments
Hello! I was wondering if someone could explain to me how to save variables to a text file? And if they could, also explain to me a bit about the purpose of a text file?
For example if I want to save these variables to a text file, would it be something like this?
x = [2:2:28] y = [3:3:43]
save ('variables.txt', x, y)
thank you for your help!
0 Comments
Answers (1)
dpb
on 13 Feb 2015
Edited: dpb
on 14 Feb 2015
"...if I want to save these variables to a text file, would it be something like this?"
save ('variables.txt', x, y)
Nope; that form will save a .mat file which is unformatted, not text. It also won't work because the functional form of the save function must specify the variables to be saved by character strings.
save('variables.txt','x','y','-ascii')
would be the form required.
save/load are far more suited to .mat files than for text; I recommend strongly against it for the purpose. Use csvwrite or one of the multitude of other specific functions for the purpose instead. Use
help iofun
and look through the list for various choices and choose something appropriate to the need at hand.
As for what they're for, if you have need to save data in a form that can be read by humans, that's the logical choice. This can, of course include automated editing or as input to other programs that require text/ASCII files or the like.
However, for simply saving data to be recalled later or for lossless transport between systems stream files (often callled "binary") are far more concise and quicker. For data staying within Matlab then as noted above the .mat file has much to be said for it.
0 Comments
See Also
Categories
Find more on Text Files 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!