How Do I Import and Average a Vector from a Notebook File in Mathematica?

In summary, you are trying to save a vector created by an algorithm in a notebook, but you are having difficulty because your computer is saying the file is not a Mathematica package file. You are also having difficulty because your short algorithm may be wrong and causing the problem. You might be better off using Put and Get to save and restore a single Mathematica expression, like a vector of numbers in {}. Alternatively, if notebook.m is a sequence of Mathematica statements, you might add a final line that exports the vector it produces into a separate file.
  • #1
brydustin
205
0
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?
 
Physics news on Phys.org
  • #2
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
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.

Bill Simpson said:
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.
 
  • #4
I'm sorry. I shouldn't have replied.
 
Last edited:
  • #5
brydustin said:
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
Code:
   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:
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.
 
Last edited:
  • #6
SredniVashtar said:
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.
 
  • #7
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
SredniVashtar said:
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.
 

FAQ: How Do I Import and Average a Vector from a Notebook File in Mathematica?

What is the "Mathematica Import Function" used for?

The "Mathematica Import Function" is a built-in function in the Mathematica software that allows users to read and import data from external files, such as spreadsheets, images, and text files, into the Mathematica environment.

What types of files can be imported using the "Mathematica Import Function"?

The "Mathematica Import Function" supports a wide range of file formats, including CSV, Excel, JSON, XML, HDF5, FITS, and many more.

Can the "Mathematica Import Function" handle large data sets?

Yes, the "Mathematica Import Function" has the capability to efficiently import and handle large data sets, thanks to its optimized algorithms and parallel processing capabilities.

How can I specify the format of the imported data using the "Mathematica Import Function"?

The "Mathematica Import Function" has a second argument that allows users to specify the format of the imported data. Users can also use options, such as "HeaderLines" and "SkipRows", to further customize the import process.

Is it possible to import data from a URL using the "Mathematica Import Function"?

Yes, the "Mathematica Import Function" has the ability to directly import data from a URL, making it easy to access and analyze data from online sources.

Similar threads

Replies
1
Views
4K
Replies
1
Views
9K
Replies
17
Views
2K
Replies
8
Views
2K
Replies
8
Views
10K
Replies
16
Views
2K
Replies
3
Views
2K
Back
Top