They're used to detect the hull, which is why Titanium is valuable for use in military submarine hulls as its non-magnetic. Fun fact, much of the Titanium used in US submarine hulls was sourced from - er - the USSR. The needed the foreign exchange more than it was worth denying the Titanium to the US.
[0] https://en.m.wikipedia.org/wiki/Magnetic_anomaly_detector