Message Boards Message Boards

Use StringReplace on a single row of a text file?

Posted 4 years ago

Hello everyone. I have a program which performs the 2d Shannon interpolation for different random variables. I have to send that code to another person who uses python. For that, and with help from people of this forum I built the following code, which construct the Shannon interpolation for each random variable as a row of a text file and then replaces the Mathematica output to look like a Python's. The problem is that for each random variable I need to separate the look of the output. My current code alters all the rows equally. I would need to change the string replace to make each row have it's number, but I haven't seen any option to do this in the documentation.

The Shannon interpolation is as follows:

savedX = Table[XposX = aaa[[All, 1]]; YposX = aaa[[All, 2]]; 
   windXVal = aaa[[All, i]];
   windXMat = Transpose[{XposX, YposX, windXVal}];
   ifuncEPSX = Interpolation[windXMat //. {x_List} :> x];
   intDataVectorEPSX = 
    Flatten[Table[{t, u, ifuncEPSX[t, u]}, {t, xmin, xmax, 
       dDeltaX}, {u, ymin, ymax, dDeltaY}]];
   leEPSX = Length@intDataVectorEPSX;
   vectorXintEPSX = 
    Table[intDataVectorEPSX[[i]], {i, 1, leEPSX - 2, 3}];
   vectorYintEPSX = 
    Table[intDataVectorEPSX[[i]], {i, 2, leEPSX - 1, 3}];
   vectorFintEPSX = Table[intDataVectorEPSX[[i]], {i, 3, leEPSX, 3}];
   interpolatedDataEPSX = 
    Transpose[{vectorXintEPSX, vectorYintEPSX, vectorFintEPSX}];
   Total[#3*sinc[(t - #1)/dDeltaX]*sinc[(u - #2)/dDeltaY] & @@@ 
     interpolatedDataEPSX], {i, 3, countMax + 2}];

And the string replace

Export["savedWindX.txt", savedX]
savedXPython = Import["savedWindX.txt"] // StringReplace[#, {"[" -> "(", "]" -> ")", "t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}] &

What I need is to replace t by m.lammda1[i]180/np.pi on the first row, m.lammda2[i]180/np.pi in the second and so on; same thing for m.phi.

Is there a way to do this? Regards. Jaime

POSTED BY: Jaime de la Mota
6 Replies

Can you post the Xdata.mat and Ydata.mat files? I can't run your code without them.

Regards,

Neil

POSTED BY: Neil Singer

Of course.

Attachments:
POSTED BY: Jaime de la Mota

I just noticed you wanted counting on the variables. You can do that using a similar approach.

i = 1; FixedPoint[
 StringReplace[#, 
   "replacementu" -> 
    StringReplace["m.lammda#[i]*180/np.pi", "#" -> ToString[i++]], 
   1] &, foo]

Repeat for each replacement variable. I used fixedpoint instead of Do so that I would not need to know how many replacements to make, Alternatively you can Count them with Count[expr,t,Infinity]

Regards,

Neil

POSTED BY: Neil Singer

Thank you again Neil for answering my questions. However, I am afraid I cannot implement what you suggest. I have been able to skip constructing the first text file by using savedXText = ExportString[savedX, "Text"]; and then applying the replacement there.

I haven't been able to implement your sugestion about the way to enumerate the row which contains the data.

I will attach the output text file with the replacements of u and v without the row and also the notebook I am working with.

Thanks again for your answer. Jaime.

POSTED BY: Jaime de la Mota

Also, you did not give enough information in your example to try anything out so I can't see what your expression looks like.

POSTED BY: Neil Singer

Jamie,

I would not export the file until after doing all the substitutions:

myString =  StringReplace[ToString[expr, CForm], { "u" -> "m.lammda[i]*180/np.pi" , 
"t" -> "m.phi[i]*180/np.pi"}]

CForm handles the parenthesis automatically, do your replacements on the whole expression, then export the string to a file (or copy and paste it into an email). The other problem you may have is u and t are common characters but you want a symbol replacement (For example Cot[x] will mess this up).

I would use different variable names or do this:

StringReplace[
 ToString[expr /. u -> replacementu /. t -> replacementt, CForm], { 
  "replacementu" -> "m.lammda[i]*180/np.pi" , 
  "replacementt" -> "m.phi[i]*180/np.pi"}]

Regards,

Neil

POSTED BY: Neil Singer
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