First you will need to determine the number of quantization levels. I am going to assume a power of two for digital convenience's sake.
nbits = 8 % 256 qantization levels
qLevels = 2^nbits
The next step will be to scale your signal to have the same magnitude as your number of bits.
signalMin = -1
signalMax = 1
scalingFactor = (signalMax-signalMin)/qLevels
signal = signal / scalingFactor
This gives you a signal ranging from -128 to 128.
You now have the choice of four functions to use:
floor() %round down to the nearest integer
round() %round to the nearest integer
ceil() %round up to the nearest integer
fix() %round towards zero
I will use round(), then scale the signal back to its original magnitude
signal = round(signal)
signal = signal * scalingFactor
You already seem to have the ability to graph down. I will leave graphing the function up to you.
Edit: To respond to abtj's comment about quant():
I wasn't familiar with quant, but it seems it would work just fine.
The second argument needs to be the value of the least significant bit. This is the same as the scalingFactor as calculated in the code above.
The scalingFactor is simply a way to scale the original signal to the size of the quantization. i.e. scale a signal from -1 to 1 volts to ±8 for a 4 bit quantization. This is to make a function like round() useful, which will only round to integer values.
This is done by taking the range of the original signal (signalMax-signalMin) and dividing it by the number of quantization levels desired.
Please point out any bugs in my code; I don't have access to MatLab to test right now.
Edited: Swapped the * and / when scaling the signal.
Edited: Added Parentheses per A_A's comment.