MATLAB: Storing a range as a variable

rangevariable

Imagine the start and end values of a range being set from user input. They could be:
a=3;
b=10;
To extract this range from an array, I simply do:
x(a:b);
But I'd like to save this range as one variable in itself, so that I can insert the range easily many places and only have to change it in one place such as:
range=a:b;
x(range);
This gives an error. MatLab will not store the a:b in this manner since : is a string and I thus am mixing strings with numbers. I could convert the a and b into a string with num2str, but then that range cannot be inserted into the x variable.
Is there a way to do this or do I have to manually put together the range with x(a:b) every time?

Best Answer

x=[0.1:0.1:2];
range=[3:10];
x(range)