That's not really something to worry about.
Generally, your spell selection should be pretty broad, with most of your spells being applicable in many situations. For example, as a magus, you will probably want a bunch of spells that can take advantage of your Spell Combat, and maybe a few buffs.
However, you don't need to worry about "not having the right spell" in a vast majority of situations. Unless your GM specifically says that the only way through a situation is to use a particular spell, you will very rarely end up hurting the party by not having the right spell prepared.
In addition, prepared spellcasters can always leave some of their slots open to be prepared later in the day. For example, a wizard with 4 5th level spell slots might prepare cloudkill and teleport, and leave the other two open. Later in the day, if it turns out that what you really need is break enchantment, the wizard can spend 15 minutes to fill one of those empty slots with that spell. That way, you can learn a bunch of situational spells that are rarely useful, and only prepare them right when you need them.
In the past ten years that I've played 3.X games, I've never seen a time when a prepared spellcaster preparing the wrong spell caused anything worse than a minor annoyance, typically easily rectified.