How to save multiple matrix in a .dat file

12 ビュー (過去 30 日間)
SM
SM 2020 年 7 月 19 日
回答済み: dpb 2020 年 7 月 19 日
First, I have three matrices which I want to save in single .dat file.
A=[1 2 3 4;5 6 7 8;9 10 11 12];
B=[13 14 15 16 20; 12 25 24 25 26; 24 26 28 29 30];
C=[2 5 5;6 9 7;2 6 4; 2 6 9; 2 9 7];
The saving of these matrices in .dat should be in such a way that it can be access easily. How can i save it?
Second, I need to access data from this .dat file but I only know that there are three matrices in this file. Size of these matrices are completely unknown. How can I get A, B and C from this .dat file?
I appreciate you favour!
Single matrix saving:
save 'myfile.dat' A -ascii % save to myfile.dat
Access data from this .dat file:
load myfile.dat % load the file

回答 (1 件)

dpb
dpb 2020 年 7 月 19 日
DON'T EVEN THINK OF IT!!!
From the documentation...
Use one of the text formats to save MATLAB numeric values to text files. In this case:
  • Each variable must be a two-dimensional double array.
  • The output includes only the real component of complex numbers.
  • MATLAB writes data from each variable sequentially to the file. If you plan to use the load function to read the file, all variables must have the same number of columns. The load function creates a single variable from the file.
The easy way to do what you need is to just save and load the variables to/from a .mat file. That's what .mat files are for.
save ABC.mat A B C % save the variables
to retrieve
load ABC % reload A, B, C
load ABC A C % reload just A and C

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by