How to save multiple matrix in a .dat file

25 views (last 30 days)
SM
SM on 19 Jul 2020
Answered: dpb on 19 Jul 2020
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

Answers (1)

dpb
dpb on 19 Jul 2020
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

Community Treasure Hunt

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

Start Hunting!