Filter löschen
Filter löschen

appending matrices to binary file - recover with fread and reshape?

2 Ansichten (letzte 30 Tage)
Filip
Filip am 9 Mär. 2017
Kommentiert: kevin sayed am 15 Mär. 2021
Hi!
I need some help on how to recover data from a binary file. I want to store a huge matrix by appending k 1000 x 9000 matrices to a binary file, but I don't know how to recover them in the right order after reading the file using fread and reshape. I want my big matrix to be of size k*1000 x 9000, i.e. the size when concatenating the matrices in the first dimension. Here is what I am doing in principle:
fid = fopen('binFile.bin','a');
for n = 1:k
data = rand(1000,9000);
fwrite(fid,data,'double');
end
bigData = fread(fid,[k*1000*9000],'double');
bigMatrix = reshape(bigData,[k*1000, 9000]);
Thankful for any pointers to solve this!

Akzeptierte Antwort

Filip
Filip am 10 Mär. 2017
Bearbeitet: Filip am 10 Mär. 2017
I figured it out myself:
By transposing the matrix before writing to file, the matrices are appended as expected. Here is the working script in case anyone else experience the same issue:
fid = fopen('binFile.bin','a');
for n = 1:k
someData = rand(1000,9000);
% transpose
data = someData';
fwrite(fid,data,'double');
end
% slurp up the file with fread
bigData = fread(fid,[k*1000*9000],'double');
% reshape
bigMatrix = reshape(bigData,[k*1000, 9000]);
  1 Kommentar
kevin sayed
kevin sayed am 15 Mär. 2021
did you have to transpose the data back after reading it to get the right matrix?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by