Clear Filters
Clear Filters

Using fprintf to export to .txt file as vertical columns

3 views (last 30 days)
I am running fprintf to export two columns to a .txt file. The first column (A) is a datestring with format HH:MM:SS and the second column (B) is just numerical values showing 2 d.p. I want both of these columns to display vertically. The script I am using for this is below;
fprintf(fid, '%s %.2f \r\n', A(:), B(:));
Running this the .txt file is formatted as below with all the time values horizontal followed by the temp values however every second temp value is not displaying as 2 d.p
14:48:4314:48:4614:48:4914:48:5214:48:5514:48:58 15.95
1.601000e+01 16.21
1.588000e+01 15.67
1.561000e+01
What do I need to change to the script to get it to display like below?
14:48:43 15.95
14:48:46 16.01
14:48:49 16.21
14:48:52 15.88
14:48:55 15.88
14:48:58 15.61

Accepted Answer

Walter Roberson
Walter Roberson on 1 Jul 2016
fmt = '%s %.2f \r\n';
data_to_write = [A(:), num2cell(B(:))] .' ; %transpose is important!
fprintf(fmt, data_to_write{:});
  6 Comments

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!