Tag Archives: Mathematica

Mathematica memo – Saving data using “FileSave”

It’s great to automatically save an output at the end of a script. It is sometimes desirable to run the script multiple times, saving the data under a new filename. It’s simple to generate new filenames using the date etc. If the script were to be used regularly by multiple people, with different file naming tastes, it maybe useful to use a dialog box to export data. This avoids people screwing with your script to add different filenames and can be done using a single line of code

Export[SystemDialogInput["FileSave",
       "/home/cswinson/Desktop/data.m"], data]

This pops up a dialog box with a preset file path and suggested filename.

Mathematica Memo – How to import multiple data files into a single variable.

So, you’ve measured something many times and need a simple way to import all the data into a Mathematica notebook. The method below works under the following assumptions:

  1. All data files are of the same format
  2. All data files have the same number of datum, arranged in the same manner (i.e. all containing the same number of rows and columns)
  3. You wish to import all files present in the folder, though this example can easily be edited to cherry pick files.

Set location of folder containing data

SetDirectory["/.../data/"];

Get the names of the files contained in the target folder

filenames = FileNames[]

Import multiple files into a nested list

alldata = Import /@ filenames;

Note: /@ is shorthand for Map[] which performs element wise operations.

For files containing a single column you get an n x m matrix (or nested list), for files containing multiple columns you get an n x m x p 3d matrix.

Errors will result if the number of datum in each file is not consistent, though they may not appear until you try to manipulate or act on the variable alldata (see mathematica mini-memo: element wise operations).

Mathematica mini-memo: Including time stamps in filenames

The following saves data with a time stamp in the filename (this code uses a dialog window to allow the user to add to the filename, e.g. 2012661131myfile.dat):

In[] >> date = DateList[]
Out[]>> {2012,6,6,11,31,16.3462}
In[] >> condate = StringJoin[IntegerString[date[[1;;5]]]]
Out[]>> 2012661131
In[] >> DialogInput[{
   Row[{ TextCell["filename /.../data/"<>condate],
     InputField[Dynamic[[fname], String],TextCell[".dat"]}],
   Row[{DefaultButton[DialogReturn[ret=1]],
     CancelButton[DialogReturn[ret=0]]}]}];
Out[]>> 
In[] >> saveas="/.../data/"<> concatdate <> fname <> ".dat"
Out[]>> "/.../data/2012661130myfile.dat"
In[] >> Export[saveas, {data1, data2}];

There may be a better way to do this (probably a glaringly obvious one), or even a built in Mathematica function. I would appreciate comments from people to whom I appear an idiot.

Women Seeking Workplaces

British, female accelerator physicist seeks position involving accelerator physics, instrumentation, data analysis, simulation etc. Comes fully equipped with PhD. and two years of postdoc. experience.

Email: christinaswinson@hotmail.com

View Christina Swinson's profile on LinkedIn

Mathematica mini memo – Element-wise operations

I find matrices in Mathematica to be a little tricky. They are not strictly matrices, more like lists of lists. Here is a short example of manipulation of a 3d matrix. If we have a file of image data representing multiple images (i.e. multiple 2d matrices)  we can manipulate them using the Map function. So for a set of data as follows

we use  Map[Image[#,"byte"]&, data] to get 


Where the function Image represents each element in the data file as a pixel, with the option “byte” specifying the data format and # representing the function argument (in this case # = data).

Mathematica Mini Memo – Exporting Graphics

Exporting graphics - To export a plot assign a variable to the plot; for example graph1=ListPlot[c], and then export using Export["filename.jpg",graph1] to save the plot as a jpeg, with the chosen file name, in whichever folder is set as default in Mathematica’s properties (to set the target directory type SetDirectory["dir"]).

Disclaimer - Since I am a true newbie the information given here is not likely to be the most efficient way of achieving things with Mathematica. I would definitely appreciate comments and corrections.

A newbie’s take on Mathematica – Matrices (lists cont.)

I was still struggling with the lists business when I came across a different way to display them. If we have two lists a and b where a={1,2,3} and b={4,5,6} we can create the matrix c={a,b}. Mathematica views this as a list of lists and the output is as follows; c={{1,2,3},{4,5,6}}. I find this a little uncomfortable to look at when dealing with larger amounts of data. MatrixForm[c] will give an output which (to me) looks much more clear.

Disclaimer - Since I am a true newbie the information given here is not likely to be the most efficient way of achieving things with Mathematica. I would definitely appreciate comments and corrections.