Thread Subject: How do I "fwrite" with variable precision?

Subject: How do I "fwrite" with variable precision?

From: J Luis

Date: 28 Apr, 2005 12:51:25

Message: 1 of 9

Hi,
I need to write a binary file with the following structure
int32,int32,int32,int16,int16,int16,int16
....
....

but fwrite only allows to use one precision type.

How can it be done?

Thanks
J. Luis

Subject: How do I "fwrite" with variable precision?

From: Ben Barrowes

Date: 28 Apr, 2005 11:32:06

Message: 2 of 9

Are you using R14? Did you do help fwrite?

 >> help fwrite
  FWRITE Write binary data to file.
     COUNT = FWRITE(FID,A,PRECISION) writes the elements of matrix A
     to the specified file, translating MATLAB values to the specified
     precision.
...
     PRECISION controls the form and size of the result. See the list
     of allowed precisions under FREAD. If either 'bitN' or 'ubitN' is
     used for the PRECISION then any out of range value of A is written
     as a value with all bits turned on.
...
     For example,

         fid = fopen('magic5.bin','wb')
         fwrite(fid,magic(5),'integer*4')

     creates a 100-byte binary file, containing the 25 elements of the
     5-by-5 magic square, stored as 4-byte integers.
...





J Luis wrote:
> Hi,
> I need to write a binary file with the following structure
> int32,int32,int32,int16,int16,int16,int16
> ....
> ....
>
> but fwrite only allows to use one precision type.
>
> How can it be done?
>
> Thanks
> J. Luis

Subject: How do I "fwrite" with variable precision?

From: Ben Barrowes

Date: 28 Apr, 2005 11:37:33

Message: 3 of 9

Sorry, I didn't read that close enough. You want to write different
precision values into one file.

Is a loop to slow? or simply multiple fwrite statements?

fwrite(fid,magic(5),'integer*32')
fwrite(fid,magic(5),'integer*32')
fwrite(fid,magic(5),'integer*32')
fwrite(fid,magic(5),'integer*16')
fwrite(fid,magic(5),'integer*16')
...


Ben Barrowes wrote:
> Are you using R14? Did you do help fwrite?
>
> >> help fwrite
> FWRITE Write binary data to file.
> COUNT = FWRITE(FID,A,PRECISION) writes the elements of matrix A
> to the specified file, translating MATLAB values to the specified
> precision.
> ...
> PRECISION controls the form and size of the result. See the list
> of allowed precisions under FREAD. If either 'bitN' or 'ubitN' is
> used for the PRECISION then any out of range value of A is written
> as a value with all bits turned on.
> ...
> For example,
>
> fid = fopen('magic5.bin','wb')
> fwrite(fid,magic(5),'integer*4')
>
> creates a 100-byte binary file, containing the 25 elements of the
> 5-by-5 magic square, stored as 4-byte integers.
> ...
>
>
>
>
>
> J Luis wrote:
>
>> Hi,
>> I need to write a binary file with the following structure
>> int32,int32,int32,int16,int16,int16,int16
>> ....
>> ....
>>
>> but fwrite only allows to use one precision type.
>>
>> How can it be done?
>>
>> Thanks
>> J. Luis

Subject: How do I

From: J Luis

Date: 28 Apr, 2005 13:44:56

Message: 4 of 9

Ben Barrowes wrote:
>
>
> Sorry, I didn't read that close enough. You want to write different
>
> precision values into one file.
>
> Is a loop to slow? or simply multiple fwrite statements?
>
> fwrite(fid,magic(5),'integer*32')
> fwrite(fid,magic(5),'integer*32')
> fwrite(fid,magic(5),'integer*32')
> fwrite(fid,magic(5),'integer*16')
> fwrite(fid,magic(5),'integer*16')
> ...
>

Yes, I finaly did it with a loop with two fwrite calls
(one for int32 and the other for int16) for each line
of the file and it is VERY slow.

Subject: How do I

From: Ben Barrowes

Date: 28 Apr, 2005 12:35:14

Message: 5 of 9

Just a thought: if you have an even number if int16's, you could combine
them into a single int32 and use a single write statement with int32's.
Your reader won't know the difference.


J Luis wrote:
> Ben Barrowes wrote:
>
>>
>>Sorry, I didn't read that close enough. You want to write different
>>
>>precision values into one file.
>>
>>Is a loop to slow? or simply multiple fwrite statements?
>>
>>fwrite(fid,magic(5),'integer*32')
>>fwrite(fid,magic(5),'integer*32')
>>fwrite(fid,magic(5),'integer*32')
>>fwrite(fid,magic(5),'integer*16')
>>fwrite(fid,magic(5),'integer*16')
>>...
>>
>
>
> Yes, I finaly did it with a loop with two fwrite calls
> (one for int32 and the other for int16) for each line
> of the file and it is VERY slow.

Subject: How do I

From: J Luis

Date: 28 Apr, 2005 15:23:01

Message: 6 of 9

Ben Barrowes wrote:
>
>
> Just a thought: if you have an even number if int16's, you could
> combine
> them into a single int32 and use a single write statement with
> int32's.
> Your reader won't know the difference.
>

Nope, there are 3 uint32 and 3 uint16.
But how do I combine the variables (two int16 into
one int32 as you suggests)?
The same tick could be used to combine all vars
into one single 18 bytes var and than writing it
(but again how?).
BTW, thats the way it would (will?) be done in C.

Subject: How do I

From: Ben Barrowes

Date: 28 Apr, 2005 15:19:10

Message: 7 of 9

I was thinking of the bit funtions bitset get, etc, but now that I think
about it, it may not be so easy. Maybe someone else knows a way to do
this. You could always create a mex function and use C to write this
data out.

If you could write out the transpose, it would be much faster. Only 6
separate write statements instead of looking through the entire matrix.


J Luis wrote:
> Ben Barrowes wrote:
>
>>
>>Just a thought: if you have an even number if int16's, you could
>>combine
>>them into a single int32 and use a single write statement with
>>int32's.
>>Your reader won't know the difference.
>>
>
>
> Nope, there are 3 uint32 and 3 uint16.
> But how do I combine the variables (two int16 into
> one int32 as you suggests)?
> The same tick could be used to combine all vars
> into one single 18 bytes var and than writing it
> (but again how?).
> BTW, thats the way it would (will?) be done in C.

Subject: How do I

From: J Luis

Date: 28 Apr, 2005 18:25:56

Message: 8 of 9

Ben Barrowes wrote:
>
>
> I was thinking of the bit funtions bitset get, etc, but now that I
> think
> about it, it may not be so easy. Maybe someone else knows a way to
> do
> this. You could always create a mex function and use C to write
> this
> data out.

Yes, that's my last resource but its extremely
anoying having to write a mex just to write a
binary file (but it seams "pouvress obliges")

>
> If you could write out the transpose, it would be much faster. Only
> 6
> separate write statements instead of looking through the entire
> matrix.

I also considered that option but again I don't
see how it would help. The point is that int32
and int16 are interleaved between records.

Anyway, Ben thanks for your help.

Subject: How do I "fwrite" with variable precision?

From: Shlomi

Date: 21 Jun, 2010 11:07:05

Message: 9 of 9

"J Luis" <jluis@ualg.pt> wrote in message <ef04635.-1@webx.raydaftYaTP>...
> Hi,
> I need to write a binary file with the following structure
> int32,int32,int32,int16,int16,int16,int16
> ....
> ....
>
> but fwrite only allows to use one precision type.
>
> How can it be done?
>
> Thanks
> J. Luis

You should use this submition:
http://www.mathworks.com/matlabcentral/fileexchange/2055-uwriteuread

First you write your varibles to a single really large 'uint8' matrix and then you use 'fwrite' with 'uint8' precision once. It runs infinitly faster than a loop.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com