Remove rows from large MAT file
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi, I have a large matrix stored in Mat FIle. I loaded the matfile:
db = matfile(myFile)
and got:
db =
matlab.io.MatFile
Properties:
Properties.Source: '.MyFile'
Properties.Writable: true
Properties.ProtectedLoading: false
hs: [216817664x532 uint32]
I want to delete the lat row in hs field:
db.hs(216817664, :) = [];
But I got an error:
Requested 216817664x532 (429.7GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may
take a long time and cause MATLAB to become unresponsive.
Is there any other way to remove rows from such large files?
댓글 수: 4
Ive J
2021년 8월 26일
Have you tried tall arrays?
db = matfile(myFile)
t = tall(db.hs);
t(216817664, :) = []; % see write doc for saving this tall array
답변 (1개)
Vedant Shah
2025년 6월 20일
MATLAB attempts to load the entire variable ‘hs’ into memory when a row deletion is requested but given the size of the matrix, this operation is not feasible.
A possible workaround is to create a new MAT-file that excludes the last row of the original matrix. To avoid memory overflow issues, the data should be copied in manageable chunks. The following code snippet demonstrates how this can be achieved:
src = matfile('MyFile.mat');
dst = matfile('NewFile.mat', 'Writable', true);
chunkSize = 10000;
numRows = size(src, 'hs', 1);
numCols = size(src, 'hs', 2);
for i = 1:chunkSize:(numRows - 1)
lastRow = min(i + chunkSize - 1, numRows - 1);
dst.hs(i:lastRow, :) = src.hs(i:lastRow, :);
end
This approach ensures that only a portion of the data is loaded into memory at any given time, making the process efficient and scalable for very large datasets.
For more information, please refer to the documentation using the following commands in the MATLAB command line:
web(fullfile(docroot, " /matlab/ref/matlab.io.matfile.html"));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!