MATLAB: Is it possible to use “userpath” as a normal variable in MATLAB 7.6 (R2008a)

colonMATLABreturnsemisemicolon

When I execute the following code
userpath(1:end-1)
I receive the following error message:
??? Error: The end operator must be used within an array index
expression.
Also
cd userpath
gives error message:
??? Error using ==> cd
Cannot CD to userpath (Name is nonexistent or not a directory).
I would like userpath to behave like a normal variable which I can index into and also change directories based on userpath.

Best Answer

This change has been incorporated into the documentation in Release 2011b (R2011b). For previous releases, read below for any additional information:
"userpath" is a reserved function name in MATLAB and is needed to reference the MATLAB path. Creating a variable with a same name shadows the userpath function. Therefore the ability to use userpath as a normal MATLAB variable, and index or cd into it is not available in MATLAB.
To work around this issue, store the result of userpath in a variable and then perform the desired operation on it.
For instance:
a=userpath;
a=a(1:end-1);
cd(a);