Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to save data in notebooks

769 views
Skip to first unread message

Yaroslav Bulatov

unread,
Oct 5, 2007, 4:57:41 AM10/5/07
to
What is the recommended way of removing dependence on external files?
Consider the following code

data=Import["largedatafile.csv"];
ListPlot[data]

I'd like to have "data" saved in the notebook, so that when I open it,
I can replot the data. My current solution is to copy and paste the
data manually into a separate mathematica cell (ie, data={1,2,3...}),
but that causes the front-end to freeze when largedatafile.csv is too
large.

Yaroslav


Szabolcs Horvát

unread,
Oct 6, 2007, 4:40:35 AM10/6/07
to

Put the data file into the same directory where the notebook is, and use
NotebookDirectory[] to find its location. This way the data file and
notebook can be moved around freely.

--
Szabolcs

John Fultz

unread,
Oct 7, 2007, 4:09:23 AM10/7/07
to
On Fri, 5 Oct 2007 04:51:15 -0400 (EDT), Yaroslav Bulatov wrote:
> What is the recommended way of removing dependence on external files?
>
> Consider the following code
>
> data=Import["largedatafile.csv"];
> ListPlot[data]
>
> I'd like to have "data" saved in the notebook, so that when I open it,
> I can replot the data. My current solution is to copy and paste the
> data manually into a separate mathematica cell (ie, data={1,2,3...}),
> but that causes the front-end to freeze when largedatafile.csv is too
> large.
>
> Yaroslav

Assuming you're using version 6, you could get a compressed form by doing...

Compress[data]

...then modify the output cell by putting the caret at the beginning and typing

data=Uncompress@

which will convert the Output cell into an Input cell you can evaluate in the
future. It's not quite as fast and convenient as DumpSave, of course, but if
you really insist on keeping the data in the notebook, it'll work.

Now it turns out that the graphic resulting from the ListPlot[] has all of the
data you passed into it, in the aforementioned compressed form embedded in it.
You can easily send that graphic back to the kernel to pick apart the expression
(which is automatically uncompressed...no Uncompress[] command is required) and
pull the data back out of it, without requiring the additional copy of the data
my above procedure would create.

Assuming you haven't used any exotic options to ListPlot (most noticeably,
PlotMarkers, which will considerably change how the data are represented in the
graphic), then typing the following input around the graphic (i.e. place the
caret before the graphic and type, then after, and type, or using copy/paste to
insert a copy of the graphic into a new Input cell) will get what you want...

Cases[ <graphic> , Point[{x__}]:>x, 2]

Sincerely,

John Fultz
jfu...@wolfram.com
User Interface Group
Wolfram Research, Inc.

AES

unread,
Oct 8, 2007, 12:01:34 AM10/8/07
to
In article <fea47j$i20$1...@smc.vnet.net>, John Fultz <jfu...@wolfram.com>
wrote:

> > I'd like to have "data" saved in the notebook, so that when I open it,
> > I can replot the data. My current solution is to copy and paste the
> > data manually into a separate mathematica cell (ie, data={1,2,3...}),
> > but that causes the front-end to freeze when largedatafile.csv is too
> > large.
> >
> > Yaroslav
>
> Assuming you're using version 6, you could get a compressed form by doing...
>
> Compress[data]
>
> ...then modify the output cell by putting the caret at the beginning and
> typing
>
> data=Uncompress@
>
> which will convert the Output cell into an Input cell you can evaluate in the
> future. It's not quite as fast and convenient as DumpSave, of course, but if
> you really insist on keeping the data in the notebook, it'll work.

John,

Thanks very much. This is one of those very useful tricks that might be
difficult for an ordinary user to develop by himself, but can be
understood and used once you've seen it.

As an extension to it: Suppose the data in question is not necessarily
a Plot, maybe just a List; said List is the Output cell created by
executing an Input cell; and each time one executes said Input cell one
wants to save the new result **and not erase any of the old ones** that
may still be sitting there -- except of course one doesn't want to go
too far in doing this if the List is large, and one will want to delete
selected older Saved Lists from time to time.

I think I can actually figure out a procedure that can do this task by
myself (maybe add a random name or date to the List name each time you
Save one?). But of course if someone more skilled than me wanted to lay
out a template for doing this, I'd be delighted to use it instead.

Yaroslav Bulatov

unread,
Oct 9, 2007, 5:42:18 AM10/9/07
to
Thanks for the tips. "Compress" helps, but it can still take a long
time for the FrontEnd to render the output cell. Is it possible to
tell Mathematica to collapse the output cell without displaying it?

On Oct 7, 9:01 pm, AES <sieg...@stanford.edu> wrote:
> In article <fea47j$i2...@smc.vnet.net>, John Fultz <jfu...@wolfram.com>

John Fultz

unread,
Oct 10, 2007, 4:47:33 AM10/10/07
to
Some of the delay is the transfer of the data over the link, and you're not
going to avoid that. But you can deal with any delay in displaying the result
by doing something like this...

With[{x = Compress[data]},
Interpretation["<<Compressed Data>>", Uncompress[x]]]

Note...it doesn't matter what the first argument of Interpretation is, as this
merely defines the appearance.

Sincerely,

John Fultz
jfu...@wolfram.com
User Interface Group
Wolfram Research, Inc.

0 new messages