1
$\begingroup$

Reviewers: please see the comments below my answer for additional information on why the edits were made. -bobthechemist

I have a text file (with a .txt extension) written for Mathematica, but opening it does not render the graphics and simulations found in the notebook. How do I get the original notebook format back?

This problem can occur when downloading a notebook from an internet source; sometimes a notebook or cdf gets saved as a text file and the correct extension gets lost. Is there a way to programmatically open a text file containing notebook information without resorting to changing the extension?

$\endgroup$
  • $\begingroup$ tt = Import["~/test.txt"] $\endgroup$ – chris Oct 20 '14 at 11:29
  • 1
    $\begingroup$ change the extension to nb? $\endgroup$ – george2079 Oct 20 '14 at 11:43
  • 1
    $\begingroup$ posting the first dozen or so lines from the file might be useful... $\endgroup$ – george2079 Oct 20 '14 at 11:49
  • 1
    $\begingroup$ This is not really answerable without more info, optimally the file in question. Please add more detail to save us from too much speculation. $\endgroup$ – Yves Klett Oct 20 '14 at 13:33
  • $\begingroup$ There ain't such thing as a ".txt file written for Mathematica". $\endgroup$ – Sjoerd C. de Vries Oct 21 '14 at 10:58
7
$\begingroup$

I'm going to guess at what the OP wants. This code should reproduce the problem as I see it.

SetDirectory[NotebookDirectory[]]
p = Plot[Sin[x], {x, 0, 4 Pi}]
Export["test.m", p]
RenameFile["test.m","test.txt"]

The file "test.txt" is now a text version of a notebook file that has valid Mathematica code but an extension that will be misinterpreted by Mathematica. To circumvent this problem, specify the filetype in the Import

Import["test.txt","NB"]

...and all is well.

| improve this answer | |
$\endgroup$
  • $\begingroup$ bob, this seems like a good interpretation and possibly useful information for future readers. Would you care to edit the question in an attempt to salvage it, which will put it in the reopen review queue? If not I shall delete this in routine clean-up. $\endgroup$ – Mr.Wizard Feb 9 '16 at 8:33
  • $\begingroup$ @Mr.Wizard let's see what I can come up with $\endgroup$ – bobthechemist Feb 9 '16 at 12:48
2
$\begingroup$

Not sure chris has the right idea. I assume your file is not some exported data, strings, tables, etc. but rather a bunch of commands you basically want to execute from file, right? The Import command tends to work with the types of files produced by Export. It would probably treat your file as text.

You can use the Read or ReadList commands. If you use Read it will step sequentially through each line of the text file executing each line. If you do ReadList it will do them all at once. For example, I create a text file with the following contents:

Print["asdf"];
Plot[x,{x,1,10}];
Plot[x^2,{x,1,10}]
x=3;

Then when I do ReadList["C:\\test.txt"] I will get the following:

(* Output:

   asdf
   {Null,Null,[Graphics Object Here],Null}

 :End Output *)

The asdf comes from the Print command, which is executed and does work. The list is all the output, which includes Null for the lines ending with a semicolon. However, the graph of x^2 does show, because it does not have a semicolon.

Another way to do this might be:

While[Read["C:\\test.txt"] != "EndOfFile"]

which reads everything quietly -- no output because whatever output there is (be it Null or something else), it will just be compared to "EndOfFile" and not returned. That includes the Print command, which produces no output either.

After doing any one of these methods, if I ask for the value of x I will get 3 as expected.

Edit: george2079 suggests changing the extension and opening the file with Mathematica. That too is possible, if all you want to do is turn a .txt file into a .nb file. I'm not sure that's what you want either, but if so, then that works.

| improve this answer | |
$\endgroup$
  • $\begingroup$ NotebookFileLineBreakTest NotebookDataPosition[ 157, 7] NotebookDataLength[ 117810, 2882] NotebookOptionsPosition[ 115658, 2807] NotebookOutlinePosition[ 116002, 2822] CellTagsIndexPosition[ 115959, 2819] WindowFrame->Normal*) (* Beginning of Notebook Content ) Notebook[{ Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ RowBox[{"(", " ", RowBox[{ RowBox[{"HOMEWORK", " ", "2"}], " ", "-", " ", RowBox[{"LUCIE", " ", "DAVIDOV[CapitalAAcute]", " ", "1"}]}], " \ ", "*)"}], " ", $\endgroup$ – and Oct 21 '14 at 6:52
  • $\begingroup$ This is a short excerpt I want to open with Mathematica, maybe it becomes more clear now what i want ;) I want to execute this commands but it doesn't work by executing, i think i have to convert this file first but don't know how... $\endgroup$ – and Oct 21 '14 at 6:52
  • $\begingroup$ or this one too: Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"RSolve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"a", "[", "n", "]"}], "==", RowBox[{ RowBox[{"6", " ", RowBox[{"a", "[", RowBox[{"n", "-", "1"}], "]"}]}], "-", RowBox[{"9", RowBox[{"a", "[", RowBox[{"n", "-", "2"}], "]"}]}], "+", "8"}]}], "}"}], ",", RowBox[{"a", "[", "n", "]"}], ",", "n"}], "]"}]], "Input"], $\endgroup$ – and Oct 21 '14 at 6:52
  • $\begingroup$ Those are the contents of your file?? Seems like that's just a .nb file that seems to be renamed .txt. You should follow george2079's suggestion and rename it with the correct file extension... $\endgroup$ – Kellen Myers Oct 21 '14 at 9:44
  • $\begingroup$ i renamed it but it's still the same content, can't get any results out of it when i run it... $\endgroup$ – and Oct 21 '14 at 12:20

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.