I'm trying now to extract in Matlab information from android app that records output of accelerometer, gyrometer and magnetometer. As I need to find linear acceleration, I assumed that I can calculate it from accelerometer taking gravity into account. I thought that it should be possible to find by checking phone orientation based on magnetometer output (Earth magnetic field). But I can't get correct results with any formula. Am I right that it's possible to find gravity vector thanks to magnetometer? If so, how should I interpret sensor readings?
No, it is not possible to find the gravity from the magnetometer.
What you are looking for is called sensor fusion. I have implemented sensor fusion for the Shimmer platform based on this manuscript, it's basically a tutorial:
Direction Cosine Matrix IMU: Theory
Another resource I highly recommend is Sensor fusion on Android devices.
But why are you doing it yourself? The SensorManager already offers what you want.