2
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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 transplate between Matlab and Mathematica. $\endgroup$ – Henrik Schumacher Jan 14 '19 at 22:33
  • $\begingroup$ I really like this suggestion Henrick! I makes a lot of sense in how to now use the data I've imported. $\endgroup$ – Bo Johnson Jan 16 '19 at 18:55
  • $\begingroup$ I am glad that this helped you. Welcome on Mathematica.StackExchange and have fun with Mahematica! $\endgroup$ – Henrik Schumacher Jan 16 '19 at 21:15
1
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Bo Johnson Jan 15 '19 at 0:07
  • $\begingroup$ @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? $\endgroup$ – Henrik Schumacher Jan 16 '19 at 21:20
  • 1
    $\begingroup$ That's a really neat way to visualize what's happening here, thanks for pointing it out @HenrikSchumacher! $\endgroup$ – Bo Johnson Jan 16 '19 at 22:42
0
$\begingroup$

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.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.