using fread() to read part of a file
Show older comments
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 Comment
Note that it is more robust to avoid using i and j for variable names, as these are both names of the inbuilt imaginary unit.
Accepted Answer
More Answers (0)
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!