saving matrices to text files

3 次查看(过去 30 天)
Grace
Grace 2015-2-13
编辑: dpb 2015-2-14
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!

回答(1 个)

dpb
dpb 2015-2-13
编辑:dpb 2015-2-14
"...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.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by