using fread() to read part of a file
27 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ahmed
am 26 Jun. 2015
Kommentiert: Stephen23
am 26 Jun. 2015
Hi everyone I came with another frustrating question at least for me which is: I have a matrix of [10000,26] stored in a file using fwrite() function. This means I have to use fread() to read from that file. Well the problem is that when I tried to load part of the file, lets say [100,26], the fread() keeps reading the first column. So I built a side program to see how the fread() behaves
count=1;
zz=zeros(10000,26);
for i=1:10000
for j=1:26
zz(i,j)=count;
count=count+1;
end
end
vidbin=fopen('test.bin','wb');
fwrite(vidbin,zz,'float');
fclose(vidbin);
I found out when I tried to load a portion of the file as
vidbin=fopen('test.bin','rb');
myarr=fread(vidbin,[100,26],'float');
fclose(vidbin);
The array myarr is not as suppose to be. it looks like 1 2601 . . . 65001 27 2627 . . . 65027 . . . myarr= . . . . . . 2575 5175 67575 It appears as if the fread() reads the first column in zz[] and divides it along the columns of myarr[] as the first row should be [1 2 3 ... 26].
How can I make fread() reads the required part of the file that I want the way I want? Waiting for ideas and solutions, please.
1 Kommentar
Akzeptierte Antwort
Stephen23
am 26 Jun. 2015
Bearbeitet: Stephen23
am 26 Jun. 2015
The documentation for fread states that "fread populates A in column order", so when you specify the size of A it keep reading along the "columns" of the file, whereas you are wanting it to stop reading after a certain number of rows.
To resolve the issue, try transposing the matrix (untested):
fwrite(vidbin, zz.', 'float');
and then specifying how many columns, not rows:
myarr = fread(vidbin, [26,100], 'float').';
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Pulsed Waveforms 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!