I have two pieces of metal which I am using as some form of gate.When I want to open the gate,the two metals should have like poles so as to repel and hence open the gate.
When I want to close the gate the two metals should have unlike poles.
How can I make any metal have only a polarity of my choice?
Are you talking about electromagnets? First of all, this won't work with "any metal", it must be a ferrous metal. By simply reversing the direction of current flow you'll change the polarity
Magnetism works over a short range. If you are talking about something like a garden gate, forget it. If you are talking about the gate moving 20mm,, it will be possible although you will need a lot of power. A motor driven solution might be better.
Frank