Hot answers tagged

38

I think you can use Names["Global`*"] to get the name: a = RandomReal[{0, 1}, 10]; SetDirectory[$TemporaryDirectory]; DumpSave["1.mx", a]; Quit[] SetDirectory[$TemporaryDirectory]; << 1.mx Names["Global`*"] (*{"a"}*)


27

If you use Import/Export to load/save .mx files, instead of DumpSave, then the variable does not get embedded in the file, and you can assign it to any variable of choice in the new session. x = RandomReal[1, {100, 100}]; Export["~/tmp.mx", x]; y = Import["~/tmp.mx"]; x == y (* True *)


21

Yes, there is! Mathematica creates a LibraryFunction when compiling to C, but puts it in a temporary directory. If you can recover the library, you can load it as often as you like! First let's define the function as in the question: generatef[opt_] := Compile[{}, Module[{j = 0}, Do[j++, {i, 10^8}]; j], CompilationTarget -> opt]; f2 = generatef["C"]; ...


12

Method 1: Enable messages for new symbols and load MX file The following code will load MX file and show a General::newsym message for each newly created symbol. On[General::newsym]; (* enable message for each new symbol *) Off[General::stop]; (* prevent stop after 3 messages *) Get["file.mx"]; (* load MX file *) On[General::stop]; Off[General::...


12

Not much different from your approach and maybe not the best/safest approach, but DumpSave helps a bit because at least you don't have to works with strings: cacheFile = FileNameJoin[{$TemporaryDirectory, "fibonacciCache" <> ".mx"}]; If[FileExistsQ[cacheFile], Get[cacheFile], fibonacci[1] = 1; fibonacci[2] = 1; fibonacci[n_Integer] := Module[{}, ...


10

$Epilog := PutAppend[DateList[], DownValues[In], "log.log"] will do what you want. It assumes the log file exists and will append to it. You could also place the PutAppend in the file end.m, where $Epilog reads from upon exit.


9

On Windows this works for me: Pause[1]; 1 + 1 (*you can add Save or Export here ofc*) NotebookSave @ EvaluationNotebook[]; Run["shutdown -t 2 -s"]; NotebookClose[] For command explanation take a look at the link Öskå has provided: SO:162305


8

Only initialization cells will be saved as uncommented code. Check Cell -> Cell Properties -> Initialization Cell and Initialization Group. You can also use Code cells (instead of Input), which have the Initialization Cell property by default. Alt-8 or Command-8 inserts a code cell. You can check that it is an initialization cell by looking at the top ...


7

In general, when you want to share a notebook and a data file, it is easier if you use reference the file from a well defined location that is either: An absolute path that depends on the OS/user such as $HomeDirectory or $TemporaryDirectory. A directory relative to the directory that the notebook is in (you can move up/down the tree starting from ...


7

ExportString[var, "RawJSON", "Compact" -> True] [[1,[1,0]],[2,[0,0,0]],[3,[2,3]],[4,[4,5,6]],[5,[7,8,9]]] Update And note the 11.2 coming up have a built-in method to do this


7

In fact, it is possible to tell Mathematica that "the notebook should automatically be saved after each piece of output generated by evaluation in it"; see documentation. Place SetOptions[$FrontEndSession, NotebookAutoSave -> True] NotebookSave[] at the beginning of a new notebook. When this code is executed, it asks the user where to save the ...


7

This answer may seem slightly off but it was posted before recent updates. The bottom line holds though. It symbolizes boxes, not expressions, it is not Subscript which is now special but SubscriptBox. You are supposed to use $x_1$ in your code but if you can't use StandardForm/2d typesetting then you have to make a round trip to boxes and back to ...


7

You can use Get/@ FileNames["save*"].


6

Generate and export Unique variables from LowLevelFunction. LowLevelFunction[ba_] := Module[{res}, res = Unique["res"]; Table[res[elem] = If[ToString[ba] == "yes", X, 2 X], {elem, {one, two}}]; res] These variables are not temporary, and you should be able to save them


6

The following function finds a notebook (a .nb file) with a given maximum age (in days) in a given directory or its subdirectories: notebookSearch[dir_String, age_?NumericQ] := Select[ FileNames["*.nb", {dir}, Infinity], First[DateDifference[FileDate[#], DateList[], "Day"]] <= age & ]


6

There is a way to tell Mathematica to remember, in its File->Open list, long list of files you have opened and not the default of 8. The above is from options. Simply change the value from 8 to say 100, and now you will have all those files remembered. I used to have the same problem as you until I found this option.


6

You can add at the beginning or at the end of your notebook this command: NotebookSave[]


6

Add this simple code before u run everything will let Random generate exactly the same result every time you run. If you're careful enough, you may find this in lot's of posts with randomly generated input. SeedRandom["Whatever you write here, keep it the same in multiple runs!"] Hope this can help you!


6

This will call TraceScan and save each line (WriteLine) to a text file. A time-out can be defined (default 1 minute), and the function returns a clickable File for seamless opening. ClearAll[TraceToFile] SetAttributes[TraceToFile, HoldFirst] TraceToFile[expr_, filename_String, timeout_: 60] := With[ { file = OpenWrite[filename] }, Echo[timeout, "...


6

You could just save global variables and Subscript: Save[file, {"`*", "Subscript"}] If you really want to use the Notation package, then the problem is that the functions that know how to convert back and forth between the symbolized and unsymbolized forms are not being saved. Here are your definitions: Get["Notation`"] Symbolize[ParsedBoxWrapper[...


5

I use a lot of superscripts and subscripts in my work as well, so this question intrigued me although the problems I have are a little bit different (chemistry formulas and suchlike). I found a good lead from the answer by Mr. Wizard on this question (Paste data into Mathematica with formatting). I have merely modified the end of Mr. Wizard's raw paste ...


5

If your notebook is hung/non-responsive, and you haven't saved changes, don't give up! You can try to kill the kernel processes without killing Mathematica and revive it. I just did this on MacOS using the Activity Monitor program. I was evaluating my notebook, and it got stuck and stopped responding. I saw WSMKernelX and WolframKernel processes, ...


5

The answer to your first question is: "yes". Try this, for example: This is such a matrix: m = {{1, 4, 0, 0}, {3, 4, 1, 0}, {0, 2, 3, 4}, {0, 0, 1, 3}}; This makes plots of its columns: ListPlot[#, PlotStyle -> PointSize[0.03]] & /@ Table[Transpose[m][[i]], {i, 1, Length[m]}] with the effect The answer to your second question is: "yes". This ...


5

<< is one of a few special operators which turn everything that follows into a string, without having to use quotes. Thus << asd is just another notation for Get["asd"]. Note that the first form has no quotation marks. Illustration: The answer to your problem is: use the Get form, and not <<, if the file name is computed as an ...


5

Get /@ StringTemplate["save``"] /@ Range[1, 5]


5

You need to use symbols or lists of symbols as the second argument of Save. So for: f[a][x_] := x^2 you would use: Save["test.m", f] Check: Import["test.m", "String"] "f[a][x_] := x^2 "


5

You can use Save in this way: func[i_] := func[i] = i; Do[func[i], {i, 1, 3}]; FullDefinition@func (* func[1] = 1 func[2] = 2 func[3] = 3 func[i_] := func[i] = i *) Save["out.m",func]; ClearAll[func]; Get["out.m"]; FullDefinition@func (* func[1] = 1 func[2] = 2 func[3] = 3 func[i_] := func[i] = i *) So, here, we show that memoization can be used as ...


4

Performing a couple tests it appears to be a notebook option that is causing the issue. Wiping all notebook options seems to fix the issue along with calling the function above. I will post an update if I can determine any more information. deleteCache[x_String] := Module[{nb, nb2}, nb = NotebookOpen[x]; nb2 = CreateDocument[NotebookGet[nb][[1]]]; ...


4

Exporting the NonlinearModelFit result objects as mx seems to work fine. SetDirectory[NotebookDirectory[]]; ClearAll[dat0, nmf0, pcit0]; dat0 = Table[{x, (x + 3)/(x + 2) + RandomReal[{-0.01, 0.01}]}, {iSet, 1, 10, 1}, {x, 0, 5, 0.01}]; Dimensions@dat0 nmf0 = Table[NonlinearModelFit[dat0[[iSet]], (x + a1)/(x + a2), {a1, a2},x], {iSet, Length@dat0}]; pcit0 ...


4

This is exactly the automatic behavior when using .m ("package") files, rather than .nb (notebook) files, in the Mathematica front end. You can enter and evaluate input cells (which are Code-style cells in .m files rather than Input-style cells) as usual, and the output cells display in the .m file, as for a notebook file. The output cells never save with ...


Only top voted, non community-wiki answers of a minimum length are eligible