how to write ASCII data using FWRITE function

  • Follow


I am trying to write a file with both binary and ASCII data. 

I managed to write my binary data with below codes:-

fid = fopen('CEOS.mat', 'wb');

value = 555
Write = typecast(uint16(value),'uint16');
b       = fwrite(fid, Write, 'uint16');

value = 10
Write = typecast(uint8(value),'uint8');
b           = fwrite(fid, Write, 'uint8');

fclose(fid)


I wanted to add in ASCII data, how can I write into the same file? can I use fwrite function too?

HELP!
0
Reply Darlling5147 11/24/2010 7:52:05 AM

"Darlling5147 Sew" <haha_1984@yahoo.com> wrote in message <icig74$52t$1@fred.mathworks.com>...
> I am trying to write a file with both binary and ASCII data. 
> 
> I managed to write my binary data with below codes:-
> 
> fid = fopen('CEOS.mat', 'wb');
> 
> value = 555
> Write = typecast(uint16(value),'uint16');
> b       = fwrite(fid, Write, 'uint16');
> 
> value = 10
> Write = typecast(uint8(value),'uint8');
> b           = fwrite(fid, Write, 'uint8');
> 
> fclose(fid)
> 
> 
> I wanted to add in ASCII data, how can I write into the same file? can I use fwrite function too?
> 
> HELP!

I am not sure what you try to achieve.
Maybe this is what you need:

value = 'abcdefgh' ;
Write = cast(value,'uint8');
b = fwrite(fid, Write, 'uint8');

Hope this helps,

Han
0
Reply Han 11/24/2010 11:32:10 AM


"Darlling5147 Sew" <haha_1984@yahoo.com> wrote in message <icig74$52t$1@fred.mathworks.com>...
> I am trying to write a file with both binary and ASCII data. 
> 
> I managed to write my binary data with below codes:-
> 
> fid = fopen('CEOS.mat', 'wb');
> 
> value = 555
> Write = typecast(uint16(value),'uint16');
> b       = fwrite(fid, Write, 'uint16');
> 
> value = 10
> Write = typecast(uint8(value),'uint8');
> b           = fwrite(fid, Write, 'uint8');
> 
> fclose(fid)
> 
> 
> I wanted to add in ASCII data, how can I write into the same file? can I use fwrite function too?
> 
> HELP!

You can use fprintf to write to the same file.
0
Reply Cris 11/24/2010 1:54:04 PM

On 24/11/10 1:52 AM, Darlling5147 Sew wrote:
> I am trying to write a file with both binary and ASCII data.
> I managed to write my binary data with below codes:-
>
> fid = fopen('CEOS.mat', 'wb');
>
> value = 555
> Write = typecast(uint16(value),'uint16');
> b = fwrite(fid, Write, 'uint16');
>
> value = 10
> Write = typecast(uint8(value),'uint8');
> b = fwrite(fid, Write, 'uint8');
>
> fclose(fid)
>
>
> I wanted to add in ASCII data, how can I write into the same file? can I
> use fwrite function too?

fwrite(fid, 'They weep not, and neither do they toil');


The main difference between a binary file and a text file is in the 
handling of line terminators. With a binary file, if you
fprintf(fid, '\n') then you will get just a linefeed (character #10) at 
that position. With a text file, if you do the same thing and you are 
using Windows, then you would get a carriage return followed by a linefeed.

In Windows, there is an additional difference when reading text files: 
if an EOF character (control-Z, character #26) is encountered in text 
mode, it will be treated as indicating end of file even if there is more 
present in the file. This behaviour should not happen in Unix but I have 
not tested that point.

If you have an existing binary file and you wish to write further data 
to it but in text mode, then fopen() the file with 'at' permissions 
instead of 'wb'.

Note: it is probably a bad idea to use a .mat file extension for a file 
that is not a Matlab .mat save() file.
0
Reply Walter 11/24/2010 5:38:31 PM

3 Replies
685 Views

(page loaded in 0.189 seconds)

Similiar Articles:













7/25/2012 5:50:06 PM


Reply: