I have a computer-generated plain text file with some Wolfram Language definitions separated by newline characters - imagine it is the code you would type to a single cell:
shape1 = Polygon{{0,1},{1,2},{-5,6}};
shape2 = Polygon{{0,-1},{5,-3},{5,9}};
I'd like to programmatically open this file in a notebook and evaluate the code so that shape1
and shape2
are accessible in this notebook. I tried different combinations of CreateDocument[]
, Import[]
and EvaluateNotebook[]
but nothing works the way I'd like to.
What should I do? It is possible to make some changes in this generated file, since it's done by a program of mine, but I would prefer not to.
string = "shape1 = Polygon[{{0,1},{1,2},{-5,6}}]; shape2 = Polygon[{{0,-1},{5,-3},{5,9}}];"
. Having the correct string,ToExpression[string]
works fine. Actually,Get[<<filename>>]
should also work once the syntax has been corrected. $\endgroup$ToExpression@Import["thefile.nb","Text"]
did it's job, thanks. I would give +1 but I cannot yet. $\endgroup$