# Mathematica Import Function

1. Mar 28, 2011

### brydustin

hi......

I have written a Notebook.nb file which generates a vector according to certain criteria.... and
I need to repeatly re-import the last value (the vector) from the notebook, and the vector is kinda-random (so I need to rerun the first file each time I call it in my new code) and then average it in this new notebook. (Below is the entire code, minus the file being imported).

AverageSoln= 0;
Experiments= 10;

For[Calculations = 0, Calculations < Experiments,
Import["Notebook.m"];
AverageSoln= AverageSoln + %, Calculations++]

AverageSoln= Mean[AverageSoln];

I'm having difficulty getting it to work, as my computer is saying its not a Mathematica file?!
But I also have Matlab on my computer and was under the impression that I needed to convert the file to a Mathematica package file (takes the form Package.m) to work; is it possible that Matlab is not letting Mathematica access this file as it has the file.m format?

Also is my short algorithm possibly just wrong and causing the problem?

2. Mar 28, 2011

### Bill Simpson

Import seems typically used for non-Mathematica-notebook file formats.

Since you just need to save and restore a single Mathematica expression, perhaps Put and Get would be helpful to you. These write and read a single well formed Mathematica expression, like a vector of numbers in {}.

In[1]:= myvector={1,Pi,E,-4};Put[myvector,"myfile"]

Then later, perhaps even in a different run of Mathematica

In[3]:= newvector=Get["myfile"]
Out[3]= {1,Pi,E,-4}

And again, perhaps in the same or a different run of Mathematica

In[4]:= anothervector=Get["myfile"]
Out[4]= {1,Pi,E,-4}

If necessary you can provide a path to the file inside those quotes, but that brings in Mathematica's and your operating system's idea of how to specify a path.

Repeated use of Put will over write what was in the file.
!erase myfile
doesn't delete the file under my OS, for reasons I can't track down at the moment, but
<< "!erase myfile"
does. Or you can manually delete the file outside Mathematica if need be. As always, use appropriate caution when fumbling around telling an OS to delete files.

3. Mar 29, 2011

### brydustin

Not quite what I'm looking for. I need to recall the program each time in a loop and the output of the program needs to be different each time. If I use Put and Get then it will only Put one value in the file and therefore only be able to Get one valued vector; the vector is generated by a specific algorithm and needs to be pulled on demand.

4. Mar 29, 2011

### Bill Simpson

I'm sorry. I shouldn't have replied.

Last edited: Mar 29, 2011
5. Mar 29, 2011

### SredniVashtar

Where is the vector stored?
Try this, for a start, so we can see what error message do you get
Code (Text):

Get["Notebook.m"]

Some considerations:
Notebook.m is a package, not a notebook.
But then you seem to suggest it's a (successfully converted?) matlab file.
I am not up to date with the latest version of Mathematica, but I would not use Import to execute code and get its returned result. AFAIK, used in that way, Import would import the text content of the file Notebook.m and simply leave it there. It is Get[] (and also Needs[]) that can read the statements and evaluate them. Yet, if you created a package there could be problems with the scope of the variables used and returned by it.
If notebook.m is a sequence of Mathematica statements, I'd suggest you add a final line that exports the vector it produces into a separate file, and then you Import that single file (storing it, for example in the variable currentresult, like in currentresult=Last[Import[vectorsolution]] ). But then you would be better off by importing the package with the procedure that does the calculation and use the procedure directly in the remaining code. Like in
Code (Text):
Needs["myPackage.m"]; (*contains the definition of computingProcedure *)
(* you only need to import the package once *)
(* then you can evaluate computingProcedure inside a 'loop' *)
AverageSoln = Mean[Table[ computingProcedure[], Calculations]]
If notebook.m is a pure matlab file, then you should add a few lines to convert its result into a mathematica-likeable form, e.g. a list with curly bracket (although that is not strictly necessary) and then importing that list after running the m-file in matlab (Run could be of some help here).

If notebook.m is a converted matlab file, oh my - I would very much like to see what is it that it returns.​
By the use of the for loop, it seems to me you are trying to force Mathematica out of its rule-based nature and coerce it to behave like a procedural language.
Please be more specific about the nature and content of the .m file.

Last edited: Mar 29, 2011
6. Apr 1, 2011

7. Apr 1, 2011

### SredniVashtar

ok, it is possible that 'Calculation.m' is not in current path?
Where did you store it?

IIRC, Directory[] should print out the current working directory of Mathematica. If calculations.m is not there and is not in a directory included in the path, you will get that error message.

Try copying Calculation.m in a directory you know for sure is accessible to mathematica, then try again with Get, and if you actually succeed in reading it, then we can work out how to invoke Needs, so that mathematica knows the correct context.

8. Apr 1, 2011

### brydustin

Yeah, I believe this is the problem...... I'm new to programming and so directories, paths, etc.... don't mean much to me, I think if I understood, then this would sort it out.