Message Boards Message Boards

0
|
5376 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to use and to set position for variables inside string template?

Good morning!

I am trying to program a sort of string template (StringTemplate[]) in Mathematica. However, I am having some issues to use external variables inside this template. Figure 1 illustrates the desired text output of the mentioned string template. This output is going to be used as input for another program. On this way, number and text positions should be strictly identical to the example presented in Figure 1.

Figure 1 - Desired text output. Figure 1 - Desired text output.

I have tried many ways to achieve my goal, but without success. Figure 2 presents a exemple of what I have tried:

TEST := ToString[NumberForm[0.82544479318, 11]];
CODIGO := StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`"][TEST]

The result is:

In[73]:= CODIGO

Out[73]= "
C ----------------------------------------------------------------------
C [R] Matrix
C ----------------------------------------------------------------------
$VINTAGE, 1,
  X0001AX0001X               0.82544479318
  X0001AX0001X0.82544479318"

PaddedForm is not working too:

In[74]:= TEST := 
  ToString[PaddedForm[NumberForm[0.82544479318, 11], 28]];
CODIGO := StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`"][TEST]

In[77]:= CODIGO

Out[77]= "
C ----------------------------------------------------------------------
C [R] Matrix
C ----------------------------------------------------------------------
$VINTAGE, 1,
  X0001AX0001X               0.82544479318
  X0001AX0001X0.82544479318"

It is important to note that numbers start in an imaginary column and "grow" to the left as illustrated in the figure below.

enter image description here

Maybe it's something easy to solve , but I'm not getting it.

Thank you in advance for any advice, tip and/or help!

--Luiz Gutierres

POSTED BY: Luiz Gutierres
4 Replies

Then try this

Clear[padit];
padit[num_] := Module[{},
Return[
StringTake[StringJoin[StringJoin[Table[" ",{28}]],
StringReplace[ToString[NumberForm[FortranForm[num], 11]],
"e"->"E"]], -28]
]
];
CODIGO=StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`
     X0001AX0001X`2`
     X0002AX0002X`3`"][padit[0.825444793182342343242342],padit[0.000002342342342341231231],padit[0.081380777833]]

Returns

"
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X               0.82544479318
     X0001AX0001X             2.3423423423E-6
     X0002AX0002X              0.081380777833                               "

After a bit of exploring StringTemplate[] and TeplateApply[] you can try

Clear[padit];
padit[num_] := Module[{}, 
    Return[StringTake[StringJoin[StringJoin[Table[" ",{28}]],
    StringReplace[ToString[NumberForm[FortranForm[num],11]],"e"->"E"]],-28]]];
TemplateApply[
StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X``
     X0001AX0001X``
     X0002AX0002X``", InsertionFunction -> padit],{0.825444793182342343242342,0.000002342342342341231231,0.081380777833}]

Returns same result as above just a bit clearer.

Good luck

POSTED BY: Hans Michel

Thank you very much for your help, Hans Michel.

The problem is solved!

POSTED BY: Luiz Gutierres

Thank you very much for your help, Hans Michel!

I tried to generalize your solution for different possibilities of numbers. They should look like Figure 1, this is why there is a sequence of PaddedForm(s) and NumberForm(s) below. Numbers should be displayed in a Fortran?like form with 11 precise digits. However, I think it get lost during the different kinds of "Form".

In[205]:= ATESTa := 0.825444793182342343242342
ATESTb := 0.000002342342342341231231
ATESTa11E := 
 PaddedForm[ATESTa, 11, NumberFormat -> (Row[{#1, "E", #3}] &)]
ATESTb11E := 
 PaddedForm[ATESTb, 11, NumberFormat -> (Row[{#1, "E", #3}] &)]
TESTa := ToString[
  NumberForm[ATESTa11E, 26, NumberPadding -> {" ", ""}]]
TESTb := ToString[
  NumberForm[ATESTb11E, 26, NumberPadding -> {" ", ""}]]
CODIGO = StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`
     X0001AX0001X`2`"][TESTa, TESTb ]

Out[211]= "
C ----------------------------------------------------------------------
C [R] Matrix
C ----------------------------------------------------------------------
$VINTAGE, 1,
  X0001AX0001X               0.82544479318
  X0001AX0001X 0.82544479318E
  X0001AX0001X 2.3423423423E-6"
POSTED BY: Luiz Gutierres

Try the following:

TEST = ToString[NumberForm[0.82544479318,26, NumberPadding -> {" ",""}]];
CODIGO = StringTemplate["
   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X`1`"][TEST]

Will return

   C ----------------------------------------------------------------------
   C [R] Matrix
   C ----------------------------------------------------------------------
   $VINTAGE, 1,
     X0001AX0001X               0.82544479318
     X0001AX0001X               0.82544479318

I'm not sure this is the full solution, as the column is of length 28, I had to use 26 to get it to align. Is it the length minus sign and decimal point locations?

POSTED BY: Hans Michel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract