Why is fprintf not printing the first whitespace in a string?

15 views (last 30 days)
Using
C = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', ' ')
I read in a file which contains both numbers and characters.
Some lines begin with negative numbers and others with postive.
The lines that begin with a postive number have an additional whitespace in the front as this type of format is essential for my application.
For example, this should be the correct format:
2.95257637E+00 1.39690040E-03-4.92631603E-07 7.86010195E-11-4.60755204E-15 E
-9.23948688E+02 5.87188762E+00 3.53100528E+00-1.23660988E-04-5.02999433E-07 E
The second line begins with a negative sign and the first a space to offset the numbers that follow.
The letter "E" at the end of each line indicates that I have the same # of characters per row, as well as the same locations for each entry.
I have veritfied that the imported cell contains the additional space in the front when beginning with a postive number.
However, when I attempt to write the modified version of the cell to a new file with
fprintf(fid,new_C{:});
The extra space in the front disappears no matter what delimeter I try.
2.95257637E+00 1.39690040E-03-4.92631603E-07 7.86010195E-11-4.60755204E-15 E
-9.23948688E+02 5.87188762E+00 3.53100528E+00-1.23660988E-04-5.02999433E-07 E
How can I modify the function such that I obtain the output I desire?
Thank you

Accepted Answer

Bruno Luong
Bruno Luong on 9 Sep 2019
Edited: Bruno Luong on 9 Sep 2019
Read carefully FPRINTF document; the first argument after optional fid is a a FORMAT; not arguments. So you must add '%s'
fprintf(fid,'%s',new_C{:});
  5 Comments
Bruno Luong
Bruno Luong on 9 Sep 2019
Edited: Bruno Luong on 9 Sep 2019
Have you tried
fprintf(fid,'%s\n',new_C{:,1});
? Should be equivalent to your for-loop.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!