Here i am giving you some idea about S functions and how to start writing them.
SOme examples are given which will help you get a picture. Once you are through thisthen you will able to understand and refer MATLAB documentation in a better way.This is meant to be a quick start guide and not a full fledged tutorial.