Fread format causes errors

2 views (last 30 days)
Dylan
Dylan on 17 Sep 2013
Hi,
So I have a simple program that uses fread three times to read the data from a 3x3 matrix of numbers (.mat format). Each time it reads, I want it to read using a different size output matrix. However, the third time I read, it outputs a blank 1 cell matrix. This happens regardless of what I put into the size for that third instance of fread. Here's the code: if true % code " fid=fopen('numbers.mat','r');
sizeC1=[2, 2]; sizeC2=inf; sizeC3=[4, 4];
precision1='char'; precision2='double=>char';
c1=fread(fid,sizeC1,precision1); c2=fread(fid,sizeC2,precision1); c3=fread(fid,sizeC1,precision1);
fclose(fid); " end
Here's my outputs:. I'm not writing c2 because it is a giant vector, but seems to be behaving appropriately. " c1 =
77 84
65 76
c3 =
[]
"
On an unrelated note, how does formatting code work for these forums?
  1 Comment
Jan
Jan on 17 Sep 2013
@Dylan: Welcome in this forum! I'm very glad to see, that you care about formatting and ask actively for help. Several thousand other users did not care about a proper formatting and some hundred did not even care after they have been asked for.
You find even questions about this forum answered here, so it is worth to search for it: http://www.mathworks.com/matlabcentral/answers/?term=code+formatting
A nice summary can be found at http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup or you can click on the "? Help" link on top of the field to input a message.

Sign in to comment.

Answers (3)

Jan
Jan on 17 Sep 2013
I do not understand the intention of the code.
fread() cannot read -MAT files properly, because this format contains headers and the data can be compressed etc.
'double=>char' is a strange format converter. What do you expect as output?
Reading Inf elements from a file moves the file pointer to the end. Then a following fread() cannot read any element anymore.
It is not clear where the "3x3 matrix" appear in the code.

Image Analyst
Image Analyst on 17 Sep 2013
Why do you assume that a mat file has just a raw binary format and not some proprietary format? Try using load() instead of fread():
% Read everything back in to MATLAB.
storedStructure = load('numbers.mat');
% Save the structure members into individual variables for convenience.
c1 = storedStructure.c1;
c2 = storedStructure.c2;
c3 = storedStructure.c3;

dpb
dpb on 17 Sep 2013
Maybe it'll be easier to see w/o the obfuscation of the substitutions...what you ahove above translates to
c1=fread(fid,[2,2],'char');
c2=fread(fid,inf,'double=>char');
...
the SIZE argument of inf in the second read says fill c2 reading to EOF so, sure, anything after that will simply return empty.
BTW, if the input file really is a '.mat' format written by save then you need to use load to retrieve it as it includes more than just the data.
See the following example --
>> whos x1
Name Size Bytes Class Attributes
x1 1x10 80 double
>> save x1 % save it as .mat file
>> fid=fopen('x1.bin','w'); % then as stream...
>> fwrite(fid,x1,'real*8');
>> fid=fclose(fid);
>> d=dir('x1.*'); % and resulting file sizes...
>> d(:).name, d(:).bytes % NB: I formatted output a little
ans = ans =
x1.bin 80
x1.mat 2724
Note the .mat file contains the same 10 doubles is all but is 3X the size of the stream .bin file that is only the data.

Tags

Community Treasure Hunt

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

Start Hunting!