I used NDSolve to solve a system of ODE, and the resulting n functions y1[x], y2[x],... yn[x] were plotted with the Plot command, as follows,

Plot[{y1[x], y2[x],... yn[x]}/.%,{x,min,max}]

So I need to export the data in .txt multicolumn format. The answer to question [1] provided me a clue,

data = Cases[Plot[Sin@x, {x, 0, 2 Pi}], Line[data_] :> data, -4, 1][[1]]

however, I need the appropriate pattern/specification/etc for Cases. Or, alternatively, is there another way to export data?

Thank you

[1] Plot, extract data to a file

  • 1
    $\begingroup$ Please include the complete code, including the part that generates the answer you are substituting into plot, or alternatively generate a minimal working example with fake data that still represents the structure of your problem. What you have may not be enough detail for us to help meaningfully. $\endgroup$
    – MarcoB
    Nov 3 '21 at 12:34
  • $\begingroup$ What should "/.%" mean? $\endgroup$ Nov 3 '21 at 13:00
  • 1
    $\begingroup$ Hi @celsodad Welcome to MmaSE, there is so much to learn when one starts here. Please start by taking the tour now. It will help us to help you if you write an excellent question. Always edit your question if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. As you receive help, we hope you will give back too by voting and answering questions. Please keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$
    – rhermans
    Nov 3 '21 at 13:03
  • 1
    $\begingroup$ I think this question needs clarification. What is the motivation? Why extract data from a plot instead of using the data that created the plot on the first instance? What is the actual code used? This reduced version is not a minimum working example. The OP should show minimum due diligence, as the limited information provided requires speculation from anybody trying to offer an answer. $\endgroup$
    – rhermans
    Nov 3 '21 at 13:05
  • $\begingroup$ "Or another way to export data to .txt": Try data = RandomReal[{-4, 4}, 20] and then: Export["C:\\data.txt", data] . $\endgroup$
    – Syed
    Nov 3 '21 at 13:28

Let's say you solve the following equation:

sol = DSolve[x^2*y''[x] + 5*x*y'[x] + 6*y[x] == 0, y[x], x]
{{y[x] -> (C[2] Cos[Sqrt[2] Log[x]])/x^2 + (C[1] Sin[Sqrt[2] Log[x]])/

Create a table: modify the range and increments as required.

dexp = Table[{x, y[x] /. sol[[1]]}, {x, 0.1, 0.5, 0.05}] /. {C[1] -> 
    1, C[2] -> 2}

Try both formats:

Export["C:\\dexp_1_2.txt", dexp]
Export["C:\\dexp_1_2.csv", dexp, "CSV"]

You can extend the Table above, by adding more entries according to your solution.


Thanks a lot. I succesfully adapted the above suggestion for n equations :

sol = NDSolve[{Table[y[i]''[x] + y[i][x], {j, 1, n} == 0, {i, 1, 100}], Table[y[i][0] == i, {i, 1, n}], Table[y[i]'[0] == 0, {i, 1, n}]}, {y[1], y[2], ... y[n]}, {x, 0, 15}]

dexp = Table[{t, y[1][x], y[2][x], ... y[n][x]} /. sol[[1]], {x, 0, 15, .01}]

Export["C:\...\dexp_1_2.txt", sol]

(I used NDSolve to account for more complex ODEs)


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.