saving matrices to text files

2 ビュー (過去 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 Center および File ExchangeText Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by