using fread() to read part of a file
27 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Stephen23
le 26 Juin 2015
Modifié(e) : Stephen23
le 26 Juin 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Pulsed Waveforms dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!