MATLAB: How can i decrement a value after it reached a maximum

algorithm code decrement dac data

I have a 12 bit DAC And i want to send data by incrementing a variable from 0 to 4096 and after that continue to send data by decrementing from the Last point (4096) to the first one (0). My Code stops when 'i' reach the maximum (in my case the 'V' set from my keyboard)
Code:
For i=0:V
If i < V+1
Fwrite()
I=i+1
Else i > V-1
Fwrite()
I=i-1
End
End

Best Answer

vals = uint16([0 : V, (V-1) : -1 : 0]);
for i = 1 : length(vals)
fwrite(DEVICE, vals(i), 'uint16');
end
However, you need to pay attention to the byte order your device needs, and to how it wants to encode the 12 bits into 16 bits.