1) The slope would be NaN only if both the ‘x’ and ‘y’ variables are vectors of zeros. Otherwise, the slope would be between 0 and ±Inf.
2) I do not understand. If you want to display the linear equation, use the text function. (I can help with that, however I need more information as to what you want to do.)
I do not have the Curve Fitting Toolbox (since I do not need it for what I do in my research). This requirement was not implied or stated in the original Question you posted.
Without your data, I cannot code a similar function using the Statistics and Machine Learning Toolbox or the Optimization Toolbox functions, both of which I have and frequently use. I would need more information to provide an appropriate response.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.