2

I'm attempting to learn Mathematica to replace MATLAB. However, I have some data in .mat files which contains named variables.

enter image description here

Whenever I import the .mat file, I get a list of rules. What's the best way to go about using these? Let's say I wanted to plot some of the LabeledData, say "n2_den". Would I do something like ListLinePlot[data]/.n2_den? Or would I be better off just converting the list of rules to a list like one of the recommendations shows when I import?

Bo Johnson
  • 129
  • 5

2 Answers2

1

For your case (in a verbose style to clarify structure):

mydata = {earth_alts -> {{3}, {4}, {5}}};

myEarthAltsData = mydata[[1, 2]]

Here, mydata[[1,2]] takes the second part of the (first) association, i.e., gives just the list of values.

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • That works great, thanks! Just as a follow-up then, what does it mean doing mydata[[1,2]] in terms of rules? I get the indexing in thinking about multidimensional lists, but not rules necessarily. – Bo Johnson Jan 15 '19 at 00:07
  • @Bo, the this axiom of Mathematica is: "Everything is an expression.". And expressions are basically trees into which you can index with Part ([[...]]). In case of mydata[[1,2]] have a look at the internal representations by executing TreeForm[mydata] and TreeForm[mydata[[1, 2]]]. Do you see the pattern? – Henrik Schumacher Jan 16 '19 at 21:20
  • 1
    That's a really neat way to visualize what's happening here, thanks for pointing it out @HenrikSchumacher! – Bo Johnson Jan 16 '19 at 22:42
0

Turn it into an Association. That's the equivalent to the structure arrays (struct) in Matlab.

Btw., see here for more hints on how to translate between Matlab and Mathematica.

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309