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

  • Context: Mathematica 
  • Thread starter Thread starter brydustin
  • Start date Start date
  • Tags Tags
    Function Mathematica
Click For Summary

Discussion Overview

The discussion revolves around importing and averaging a vector generated from a Mathematica Notebook file. Participants explore methods for accessing and manipulating the vector, addressing issues related to file formats and paths, and the appropriate use of Mathematica functions for this purpose.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to import a vector from a Notebook.nb file and expresses confusion over file format compatibility, suggesting that Matlab might be interfering with Mathematica's ability to access the file.
  • Another participant proposes using the Put and Get functions to save and retrieve a vector, arguing that this method would be more suitable than Import for handling Mathematica expressions.
  • A different participant challenges the effectiveness of Put and Get for the user's needs, stating that these functions would only allow for a single value to be stored and retrieved, which does not align with the requirement for varied outputs from the algorithm.
  • One participant suggests that the user might be misunderstanding the nature of their Notebook.m file, indicating that it may not be a proper Mathematica notebook and recommending the use of Get or Needs to execute code and retrieve results.
  • Another participant raises concerns about the scope of variables when using packages and suggests exporting the vector to a separate file for easier access.
  • A participant points out potential issues with the current working directory and suggests ensuring that the Calculation.m file is in an accessible location for Mathematica.
  • One participant expresses uncertainty about directory paths and their implications for accessing files in Mathematica, indicating a lack of familiarity with programming concepts.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to import and average the vector, with no consensus reached on the most effective method. There is also uncertainty regarding the proper handling of file formats and paths.

Contextual Notes

Participants note limitations related to file format compatibility, the scope of variables in packages, and the importance of ensuring that files are located in accessible directories. These factors contribute to the challenges faced in executing the desired operations in Mathematica.

brydustin
Messages
201
Reaction score
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
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.
 
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.
 
I'm sorry. I shouldn't have replied.
 
Last edited:
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:
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.
 
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.
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
10K
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
10K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K