This is so easy, someone else should have done it already. There is some code here: http://www.mathworks.co.uk/matlabcentral/fileexchange/25528-line-clipping. However, among other things (like using input), it doesn't check for lines parallel with the clipping window borders.

Wikipedia gives a variety of algorithms -- Liang/Barsky's algorithm is probably the one which one implement most quickly.

Here goes the code:

Now, running this:

Makes this:

plot