Skip to main content
5 of 5
deleted 144 characters in body
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Struct equivalent in Mathematica?

I really miss having something like a struct in Mathematica. I know of (and regularly use) a couple of programming techniques which feel like a struct (e.g., using downvalues), but are ultimately unsatisfactory (perhaps I'm using downvalues incorrectly). What programming approaches are available which provide similar functionality to a struct?

Here's an abbreviated (and hopefully not too obtuse) example of how I use downvalues to emulate a struct. In this case, I'm distinguishing between TLC and TEC (these are sets of parameters for two different phases of a Moon mission, trans-lunar cruise and trans-earth cruise):

deadBandWidth[X][TLC] ^= 10. °;
deadBandWidth[Y][TLC] ^= 10. °;
deadBandWidth[Z][TLC] ^= 20. °;
sunSearchAngle[Z][TLC] ^= 230. °;
sunSearchRate[Z][TLC] ^= 1. °/Second;
sunSearchAngle[X][TLC] ^= 75. °;
sunSearchRate[X][TLC] ^= 1. °/Second;
safingSpinRate[TLC] ^= (360. °)/Day;
sunVector[TLC] ^= {-Cos[45. °], 0., Sin[45. °]};
safingSpinAxis[TLC] ^= sunVector[TLC];

deadBandWidth[X][TEC] ^= 20. °;
deadBandWidth[Y][TEC] ^= 20. °;
deadBandWidth[Z][TEC] ^= 20. °;
sunSearchAngle[Z][TEC] ^= 230. °;
sunSearchRate[Z][TEC] ^= 1. °/Second;
sunSearchAngle[X][TEC] ^= 75. °;
sunSearchRate[X][TEC] ^= 1. °/Second;
safingSpinRate[TEC] ^= (360. °)/Hour;
sunVector[TEC] ^= {0., 0., +1.};
safingSpinAxis[TEC] ^= sunVector[TEC];

?TLC
Global`TLC
safingSpinAxis[TLC]^={-0.707107,0.,0.707107}
safingSpinRate[TLC]^=6.28319/Day
sunVector[TLC]^={-0.707107,0.,0.707107}
deadBandWidth[X][TLC]^=0.174533
deadBandWidth[Y][TLC]^=0.174533
deadBandWidth[Z][TLC]^=0.349066
sunSearchAngle[X][TLC]^=1.309
sunSearchAngle[Z][TLC]^=4.01426
sunSearchRate[X][TLC]^=0.0174533/Second
sunSearchRate[Z][TLC]^=0.0174533/Second
Cassini
  • 5.6k
  • 5
  • 28
  • 39