## >Mathematica: import and export calculations

>

I often do long calculations in Mathematica. If I’ve got the time, I’ll keep the kernel active and do all the fun plotting, animating, basic playing-around with the results right when I’m thinking about it. However, often I need to quit and move on to other things on my todo list. That’s what this post is all about.

## .m files

Any result in Mathematica can be saved to a .m file. The syntax is pretty basic:
``` sol=First[NDSolve[{y''[t]==-y[t]-beta y'[t], y[0]==1, y'[0]==0}, y, {t,0,10}]]; Export["filepath . . ./cool.m", sol] ```
Then later you can import the file like so:
``` solnew=Import["same m file location"]; Plot[y[t]/.solnew, {t,0,10}] ```
Note how I saved the result of an NDSolve command and later was able to use it to make a plot.

## web storage

What’s extra cool is that you can save your .m files to a web server because you can do things like this:
``` coolthing=Import["http://euclid.hamline.edu/~arundquist/research/hartree/listofsolsHLi.m"] ```
and be able to use results of calculations anywhere. I especially like this when I’m going to work on a project both at home and at work as the import command syntax doesn’t need to change. I used to do this with Dropbox where both my machines would have an up-to-date copy of the .m files but I still had to change the syntax because my dropbox folder tends to be in different locations on different machines.

Professor of physics at Hamline University in St. Paul, MN
This entry was posted in mathematica, technology. Bookmark the permalink.

### 2 Responses to >Mathematica: import and export calculations

1. Fabian says:

Thanks a lot for this information. I have been simulating a robotic manipulator. and NDSOLVE can take a while. Glad to know i can save the results

• Andy "SuperFly" Rundquist says:

Glad I could help, Fabian. I recently ran into an interesting problem, though. The NDSolve took a long time and I had to go, so I did the .m file trick. However, it took a long time because it was taking incredibly small steps. The range was something like {t,0,5} modeling 20 charged particles interacting in a box. However, the .m file was over 200 MB! That’s just to save all the interpolate data. That was a little bigger than I had bargained for.