My data is
data = {{1, 1, 1, 1, 0.469258}, {1, 1, 1, 2, 0.}, {1, 1, 1, 3, -0.000123615}}
On linux-like systems there is a printf routine to make formatted print. How can I use this function from mathematica in order to obtain the formatted text file with 5 columns and 3 rows with a given format.
For instance with printf "%3i %3i %3i %3i %10.6f\n" I am expecting
1 1 1 1 0.469258
1 1 1 2 0.000000
1 1 1 3 -0.000124
Currently I doing
file = "a.dat";
If[FileExistsQ[file], DeleteFile[file]]
cmd = "printf \"%3i %3i %3i %3i %10.6f\\n\"";
Do[
args = StringRiffle[ToString[#] & /@ s];
Run[StringRiffle[{cmd, args, ">>", file}]];
, {s, data}]
But is it an idiomatic method? Will it fail in some cases, like for other variable types or too long strings? Is there a better way?
Exporting the data as a"Table"(perhaps using appropriate"FieldSeparators"as an option) work for you? $\endgroup$Exporting plus"FieldSeparators"you almost there, but formatting is difficult. I doubt that MA can exactly conform all the rules thatprintfis using, or am I wrong? I am not after the speed, but more after the control, accuracy and predictability of output. $\endgroup$ToString@*CFormon each argument so that exponentials are written in C syntax instead of Mathematica syntax, 2) if strings are going to be permitted then any characters with special meaning to the shell must be escaped. $\endgroup$