How do I write a 3D matrix to a file, and then read it back in?

조회 수: 18 (최근 30일)
Ralph Horsley
Ralph Horsley 2020년 9월 30일
댓글: Ameer Hamza 2020년 10월 1일
I'm running 2D wavefield simulations over time and need to store the wavefield at each time-step. The whole wavefield matricies over time range up to 4200 x 2000 x 2000 in size, so quite large. Storing the whole wavefield over time in matlab is taking up all of its memory, so I'm resorting to writing them to files and then reading them back in when needed for calculations. How would I go about writing the 3D matrix to a file, then reading it back in when needed?
Any help is greatly appreciated, thank you!
  댓글 수: 7
Ameer Hamza
Ameer Hamza 2020년 9월 30일
Ralph, .mat is native file format to save data in MATLAB. It store the values in exactly same precision, as stored in MATLAB workspace.
Ralph Horsley
Ralph Horsley 2020년 9월 30일
You're right, they worked perfectly, thank you!

댓글을 달려면 로그인하십시오.

답변 (1개)

Michael Croucher
Michael Croucher 2020년 9월 30일
I haven't played with the matfile function in this way before so tried a little experiment that may also serve to help you use this functionality. No idea if this is the optimal way of using it though!
If your matrices are dense and double precision then they are indeed large! Each double is 8 bytes so the matrix is
4200*2000*2000*8/(1024*1024*1024) = 125.17 % Gigabytes
Let's do a demo on a smaller version of your problem. Say, 10 pages. Here's one way of creating some data one page at a time and then saving it to the matfile.
m = matfile('myFile3.mat');
x = 4200;y=2000; %size of each matrix
N=10; % Number of pages
m.data = double.empty(x,y,0); %Initialise an empty matrix in the matfile
for i=N:-1:1
m.data(:,:,i) = rand(x,y); %There's only ever one page of the 3D matrix in memory at any one time
end
You can subsequently get any submatrix of the data without loading it all by doing, for example,
m.data(1:5,1:5,3); % the first 5 x 5 sub matrix of page 3
You may notice that I am creating the matrix inside the .mat file in reverse order - starting from N=10 and going backwards. This is because I initially tried this
m = matfile('myFile3.mat');
x = 4200;y=2000; %size of each matrix
N=10; % Number of pages
m.data = double.empty(x,y,0);
for i=1:N
m.data(:,:,i) = rand(x,y);
end
and received the error message.
Error using mymatfile (line 7)
Variable 'data' has 2 dimensions in the file, this does not match the 3 dimensions in the
indexing subscripts.
Which I wasn't expecting! It seems that setting m.data(:,:,1) makes m.data two dimensional. At this point I wonder if there is a better way to do this.
  댓글 수: 3
Michael Croucher
Michael Croucher 2020년 9월 30일
Definitely 8 bytes in a double. We can ask MATLAB:
>> a=1.0
a =
1
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
Ameer Hamza
Ameer Hamza 2020년 10월 1일
Correct, double is 8 bytes. This describes how the arrangement of data in a double variable: https://en.wikipedia.org/wiki/Double-precision_floating-point_format

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by