The field of molecular spintronics exploits the properties of organic molecules possessing a magnetic moment, either native in the form of radicals or induced by the insertion of transition metal magnetic ions. To realize logic or storage molecular spin-tronics devices, molecules with stable different magnetic states should be deposited on a substrate, and switching between the states controllably achieved. By means of a first-principles calculations, we have devised a functional molecule exhibiting different magnetic states upon structural changes induced by current injection. We investigate the prototypical case of non-planar M-Phthalocyanine (MPc), where M is a transition-metal ion belonging to the 4d and 5d series. We find that for ZrPc and HfPc deposited on a graphene decorated Ni(111) substrate, two different structural conformations could be stabilized, for which the molecules attain different magnetic states depending on the position of the M ion – whether above the Pc or between the Pc and the substrate –, acting therefore as molecular magnetic button. Our work indicates an intuitive way to engineer a magnetic molecular switch with tailored properties, starting from the knowledge of the basic atomic properties of elements and surfaces.