thank you so much for your response its good to hear some reassurance that its just anxiety. I just can't seem to get it out of my head that its happend before, it seems impossible to forget. I love my girlfriend and would be devistated if anything happend between us. Your advice makes me believe that its truely a "mind over matter" situation. Thanks.
Anxiety that it is going to happen is the problem 99% of the time, as you have already identified. As for the other couple of times, a number of things can cause it to happen. Stress, such as work/finance/family and relationship related situations can cause erectile problems like the ones you describe. Also substances you are taking can cause issues. Even things as simple as caffiene and aspirin affect some people. Anti-depressants are a big causer of problems like this.
Does it usually happen once you have already had sex before that day? It's hard on both people in situations like this. Good communication is the only way. It can be hard to talk about but it's really important that you do. Otherwise you will be stressing about going down and making her feel bad and it will become a self fulfilling prophecy.
Make sure you reassure her bout how attractive you find her, how much she turns you on etc. Be completely honest and reiterate to her that you don't know why it happened, tell her how it makes you feel when it does and make sure she understands that you worry you will go down and upset her, and that because of that worry it is exactly what happens. It's hard for her as well but she has to trust you and try to be understanding. If you can get her to understand why it is happening she won't be upset when it does, and this will help you to relax and not worry about it so much while you are having sex.
It might help if you tell her that having an erection is an indicator of your horniness but not a measure of it. That you can be honry and still lose an erection and also get an erection without being necessarily 'horny'. Though one usually follows the other :p