2
$\begingroup$

I have a do-loop in which I use recursively a NMinimize process and gives a result of the form

in: Do[NMinimize[f[i,V0,rd,d,n],{V0,rd,d,n}], {i,1,3}]

out:{V0 -> 200.700, rd -> 7.68528, d -> 0.722816, n -> 1.0}
    {V0 -> 202.828, rd -> 7.46368, d -> 0.255458, n -> 1.0}
    {V0 -> 208.434, rd -> 7.46236, d -> 0.479465, n -> 1.0}

I want to store that same result in a .txt file for later use as replacement lists, say:

VELO[V0,rd,d,n]/.{V0 -> 200.700, rd -> 7.68528, d -> 0.722816, n -> 1.0};
VELO[V0,rd,d,n]/.{V0 -> 202.828, rd -> 7.46368, d -> 0.255458, n -> 1.0};
VELO[V0,rd,d,n]/.{V0 -> 208.434, rd -> 7.46236, d -> 0.479465, n -> 1.0};

where VELO[V0,rd,d,n] is a function of the four parameters. I export the file as

Do[Write[bestfit, MINREDCHISQRAT[i][[2]]];
   , {i, 1, 3}];
  Close["bestfit.txt"]
 ]

When I attempt to do it, and later I import the .txt file

Import["C:\\Users\\psrot\\bestfit.txt", "Table"]

I get something like

    {{{V0, ->, 200.7, rd, ->, 7.68528, d, ->,0.722816, n, ->, 1.0}}, 
{{V0, ->, 202.828, rd, ->, 7.46368, d, ->, 0.255458, n, ->, 1.0}}, {{V0, ->,
       208.434, rd, ->, 7.46236, d, ->, 0.479465, n, 
      ->, 1.0}}}

which I can't use as a replacement because of the COMMAS.

My question is: How can I save the replacement lists in such a way that when I import the .txt that contains them, I could use them to evaluate my function in a do-loop for each set of parameters?

PS: I put only 3 as the steps of the do-loop, in reality I have more than 1000 steps.

$\endgroup$
  • $\begingroup$ What is the output of MINREDCHISQRAT[i][[2]] for one of the is? $\endgroup$ – Edmund Apr 5 '17 at 0:02
  • 2
    $\begingroup$ Try Import["C:\\Users\\psrot\\bestfit.txt", "Package"]. $\endgroup$ – J. M. is away Apr 5 '17 at 0:33
  • $\begingroup$ thank you very much dear @J.M. but i only manage to load the last element of the file, not the others, which i need for another loop evaluation $\endgroup$ – user115376 Apr 5 '17 at 7:23
2
$\begingroup$

I already found it, it is enough to import as:

Import["C:\\Users\\psrot\\bestfit.txt", "List"]

and then

ToExpression@ %[[i]]
$\endgroup$
0
$\begingroup$

ReadList is your friend:

ReadList["C:\\Users\\psrot\\bestfit.txt"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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