How to add formatting to a text file made with dlmwrite?
Show older comments
So I have a pretty simple code that takes a text file that looks like the following (let's say the text file is called example.txt:
Time, m
5.1,6
5.3,9
5.5,9
5.7,12
5.9,15
And using a code adjusts the time increment and creates a new text file that looks like this:
examplefinal.txt
0.06
5.100000,6.000000
5.160000,6.900000
5.220000,7.800000
5.28...
...
5.880000,17.700000
My question is with the formatting of the final file, I want to see if there's any way I can add spaces before each line of numbers, as well as a space before and after the comma in between the two columns, so it looks like this:
examplefinal.txt
0.06
5.100000 , 6.000000
5.160000 , 6.900000
5.220000 , 7.800000
...
5.880000 , 17.700000
Here's my code so far:
function [finalfile] = example(ex)
incr = 0.06 %time increment
exid = fopen(ex)
A = textscan(thid, '%f %f', 'delimiter', ',', 'headerlines', 1);
t = A{1};
m = A{2};
FullMat = [t m];
timecol1 = min(FullMat(:,1)) : incr : max(FullMat(:,1))
timecol2 = interp1(FullMat1(:,1), FullMat(:,2:end), timecol1(:))
NewTime = [timecol1(:) timecol2];
finalfile = sprintf('%sfinal.txt', ex(1:end-4));
finalfile = fopen(finalfile,'w');
frpintf(finalfile, '%s\n%f\n', finalfile, incr)
dlmwrite(finalfile,NewTime, '-append', 'delimiter', ',', 'precision', '%.6f')
end
For the delimiter, I can't just change it to ' , ' to fix the spaces in between numbers, I run the error " , is not a valid attribute or delimiter. Delimiter must be a single character."
Any help anyone can provide would be very appreciated, thanks!
Answers (0)
Categories
Find more on Web Services 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!