Register to reply

Mathematica Import Function

by brydustin
Tags: import, importing, mathematica
Share this thread:
brydustin
#1
Mar28-11, 12:35 PM
P: 209
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?
Phys.Org News Partner Science news on Phys.org
Hoverbike drone project for air transport takes off
Earlier Stone Age artifacts found in Northern Cape of South Africa
Study reveals new characteristics of complex oxide surfaces
Bill Simpson
#2
Mar28-11, 01:58 PM
P: 1,028
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.
brydustin
#3
Mar29-11, 08:42 AM
P: 209
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.

Quote Quote by Bill Simpson View Post
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.

Bill Simpson
#4
Mar29-11, 10:43 AM
P: 1,028
Mathematica Import Function

I'm sorry. I shouldn't have replied.
SredniVashtar
#5
Mar29-11, 12:49 PM
P: 94
Quote Quote by brydustin View Post
I have written a Notebook.nb file which generates a vector according to certain criteria....
Where is the vector stored?
Try this, for a start, so we can see what error message do you get
   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
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.
Also is my short algorithm possibly just wrong and causing the problem?
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.
brydustin
#6
Apr1-11, 10:27 AM
P: 209
[QUOTE=SredniVashtar;3217525]Where is the vector stored?
Try this, for a start, so we can see what error message do you get
[CODE]
Get["Notebook.m"]

This was the error I got
Get::noopen: Cannot open Calculation`. >>

I've tried multiple permutations of the name Calculation.m`, "Calculation.m", etc...
They all give the same result.


When I try
Needs["Calculation`"]
I get no error but nothing until I type the vector I need, then its always the same value unless I manually click inside of the package and run it there, and recall the vector again outside the package but again its "fixed" until I click Shift-Enter "inside" the gray area (of the package). Currently the way I have it is, "Calculation`" is inside the greater notebook and is partitioned with a gray background. It looks like this
BeginPackage["Calculation`"]
.........
EndPackage[]

Then I would like to "call" on the "Calculation`" as needed.
SredniVashtar
#7
Apr1-11, 01:13 PM
P: 94
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.
brydustin
#8
Apr1-11, 04:38 PM
P: 209
Quote Quote by SredniVashtar View Post
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.
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.


Register to reply

Related Discussions
Sum function in mathematica Math & Science Software 2
Mathematica File Import Question Math & Science Software 1
How to Import Igor Pro Binary Wave files into Mathematica? Math & Science Software 0
Recursive Function in Mathematica Math & Science Software 10
Give to an interpolation function an analitic function with mathematica Math & Science Software 1