Search code examples
juliastdio

Capturing and displaying output from within Julia


I looked everywhere for this so I am putting it here for the weary traveler;

Question: How do I capture the full output of a variable to a file from within a julia script?

i.e. :

#script.jl
y = f(x)
y > out.txt

Solution

  • The answer is here:

    https://github.com/JuliaLang/IJulia.jl/issues/455 If you want to display the output then:

    show(STDOUT, "text/plain", x)
    

    If you want to pipe the output to a file then:

    x=rand(Float32, 32,32)
    f = open("log.txt", "w")
        write(f, string(x))
    close(f)
    

    And for larger x or prettier output

    x = rand(Float32, 1028,1028);
    f = open("log.txt", "w");
    writedlm(f, x);
    close(f);