Mechanical control of magnetic states of finite carbon chains encapsulated in single wall carbon nanotubes