Register to reply 
Mathematica Import Function 
Share this thread: 
#1
Mar2811, 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 reimport the last value (the vector) from the notebook, and the vector is kindarandom (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
Mar2811, 01:58 PM

P: 1,043

Import seems typically used for nonMathematicanotebook 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
Mar2911, 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.



#4
Mar2911, 10:43 AM

P: 1,043

Mathematica Import Function
I'm sorry. I shouldn't have replied.



#5
Mar2911, 12:49 PM

P: 104

Try this, for a start, so we can see what error message do you get
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 Please be more specific about the nature and content of the .m file. 


#6
Apr111, 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 ShiftEnter "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. 


#7
Apr111, 01:13 PM

P: 104

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
Apr111, 04:38 PM

P: 209




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 