MATLAB Answers

Willam Willam

How to write all the character continuously

Asked by Willam Willam
on 1 Mar 2013

Hi guys. I got a question want to ask you guys. Below is my code.

    string = uint8(retrieveblock);
    export = sprintf('%d',string );
    export = bin2dec(export);
    char_export = char(export);
    fid = fopen('retrievedText.txt', 'w');
    fwrite(fid, char_export);

This is within a for loop. my question is why I will just write the last character into the text file. Thanks and appreciate your opinion.


Jan Simon
on 1 Mar 2013

You can edit the original question to improve the formatting of the code. I've inserted a blank line before and after the code.

Please use meaningful tags. All questions in this forum concern "Matlab".

Oops. Sorry. Did got any ideas? :)


No tags are associated with this question.


1 Answer

Answer by Jan Simon
on 1 Mar 2013
 Accepted answer

You write all characters to the file, but all former characters are overwritten continuously.

You could open the file for appending instead of overwriting:

fid = fopen('retrievedText.txt', 'a');

But it would be much more efficient to open the file once only before the loop:

fid = fopen('retrievedText.txt', 'w');
for k = 1:1000
  fwrite(fid, char_export);

  1 Comment

Thanks ya Simon. The result is what I expected. Thank you very very much

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!