1
$\begingroup$

Please help me create a .dat file with the data format is as in the link given below https://pastebin.com/CXq0N3aT And export the file. Part of the data format in the above link as follows

bag -585,10 ff  
stay    60      
exci    4   88e-6,-88e-6,2e-6   UID_GH-585bsTC
exci    4   -88e-6,88e-6,50e-6  UID_GH-585bsTCdown
bag -580,10 ff  
stay    60      
exci    4   88e-6,-88e-6,2e-6   UID_GH-580bsTC
exci    4   -88e-6,88e-6,50e-6  UID_GH-580bsTCdown
bag -575,10 ff  
stay    60      
exci    4   88e-6,-88e-6,2e-6   UID_GH-575bsTC
exci    4   -88e-6,88e-6,50e-6  UID_GH-575bsTCdown
bag -570,10 ff  
stay    60      
exci    4   88e-6,-88e-6,2e-6   UID_GH-570bsTC
exci    4   -88e-6,88e-6,50e-6  UID_GH-570bsTCdown
bag -565,10 ff  
stay    60      
exci    4   88e-6,-88e-6,2e-6   UID_GH-565bsTC
exci    4   -88e-6,88e-6,50e-6  UID_GH-565bsTCdown

All the numbers can be variables. exci, UID, and TC are also variables. I tried using the Table but it did not help to produce the same .dat file in the link. Please see below for my efforts.

\[CapitalDelta]B = 10;
Ic = N[88 10^-6];
\[CapitalDelta]Ic = N[2 10^-6];
\[CapitalDelta]Icb = N[50 10^-6];
TC = 300;
IS = exci;
ISads = 4;
SampID = UID;
tw = 60;

mylist = TableForm[
  Flatten[Table[{{"bag", {{B, \[CapitalDelta]B}}, "ff"}, {"stay", 
      tw}, {IS, 
      ISads, {{Ic, -Ic, \[CapitalDelta]Ic}}, {StringJoin[{TextString[
          SampID], "_GH", TextString[B], "bs" , 
         TextString[TC]}]}}, {IS, 
      ISads, {{Ic, -Ic, \[CapitalDelta]Icb}}, {StringJoin[{TextString[
          SampID], "_GH", TextString[B], "bs" , TextString[TC], 
         "down"}]}}}, {B, -585, -565, 5}], 1]]
Export["mylist.dat", StringRiffle[mylist, "\n"], "Table"]

Output

bag -585    10  ff  
stay    60      
exci    4   0.000088    -0.000088   2.*10^-6    UID_GH-585bs300
exci    4   0.000088    -0.000088   0.00005 UID_GH-585bs300down
bag -580    10  ff  
stay    60      
exci    4   0.000088    -0.000088   2.*10^-6    UID_GH-580bs300
exci    4   0.000088    -0.000088   0.00005 UID_GH-580bs300down
bag -575    10  ff  
stay    60      
exci    4   0.000088    -0.000088   2.*10^-6    UID_GH-575bs300
exci    4   0.000088    -0.000088   0.00005 UID_GH-575bs300down
bag -570    10  ff  
stay    60      
exci    4   0.000088    -0.000088   2.*10^-6    UID_GH-570bs300
exci    4   0.000088    -0.000088   0.00005 UID_GH-570bs300down
bag -565    10  ff  
stay    60      
exci    4   0.000088    -0.000088   2.*10^-6    UID_GH-565bs300
exci    4   0.000088    -0.000088   0.00005 UID_GH-565bs300down

This is somewhat matches with the data format but still there are few issues:

  1. There is no comma between -585 and 10
  2. 0.000088 has to be 88e-6, similarly other numbers

Hope this clarifies. Please feel free to ask if something is not clear.

$\endgroup$
6
  • $\begingroup$ Waiting for an answer. Is this not a good question ? experts- please try to help me . Thanks $\endgroup$
    – mrt31
    Commented May 1, 2022 at 2:32
  • 1
    $\begingroup$ Make the question self-contained. Many people don't like to click external links without a good reason. Moreover, the platform itself might not work in many countries due to local restrictions. Provide dat file format with your post or provide a 1-click accessible link to the file. $\endgroup$
    – Syed
    Commented May 1, 2022 at 4:05
  • $\begingroup$ @Syed , Thanks much. now edited question. Included data file format. Please let me know if I miss anything $\endgroup$
    – mrt31
    Commented May 1, 2022 at 4:51
  • $\begingroup$ It is still unclear where you are deriving each of the four entries in every sublist from. Are these randomly generated? What is your question exactly? $\endgroup$
    – Syed
    Commented May 1, 2022 at 5:14
  • $\begingroup$ @Syed sorry that it is not so clear. TableForm of the list gives me exactly how the data look like. Please sweep value -585 to -565 in steps of 5 to generate the data while replacing all other entries with some values. I would like to have the format of the data as it is . Thanks $\endgroup$
    – mrt31
    Commented May 1, 2022 at 6:04

1 Answer 1

2
$\begingroup$

Export is not your friend here, because Export is for known data formats, like CSV and JPEG. Your data format is completely custom.

The group of functions you want to use here are OpenWrite, Write, WriteString, and Close.

You start by opening a stream:

stream = OpenWrite["out.dat"];

Then you would write some utility functions, one for each type of data line.

For the bag line it could be:

writeBagLine[n_Integer, m_Integer] := WriteString["bag " <> ToString[n] <> "," <> ToString[m] <> " ff"]

Or you can use StringTemplate if you prefer:

writeBagLine[n_Integer, m_Integer] := WriteString[stream, StringTemplate["bag `n`,`m` ff"][<|"n" -> n, "m" -> m|>]]

Repeat this for the other lines (they are similar).

For the 0.00088 number formatting use NumberForm:

ToString[NumberForm[0.0000088, NumberFormat -> (Row[{#1, "e", #3}] &)]]

This gives "8.8e-6", if it needs to be more like "88e-6" (with a nonstandard mantissa, let me know)

When you're done writing to your file, you can close the stream:

Close[stream]

This prevents you from accidentally writing more content to the file.

$\endgroup$
2
  • 1
    $\begingroup$ @ Arnoud Buzing , thanks for your time. It helped me a lot. $\endgroup$
    – mrt31
    Commented May 3, 2022 at 8:05
  • 1
    $\begingroup$ Good to hear @mrt31 , feel free to my my answer an upvote! $\endgroup$ Commented May 3, 2022 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.