MATLAB: How to change the variable or observation names in the DATASET object in MATLAB 7.9 (R2009b)

Statistics and Machine Learning Toolbox

I have created a DATASET object. I would like to change the variable or observation names of this dataset. The code I use to create the DATASET object is from the documentation for the DATASET constructor:
load fisheriris
NumObs = size(meas,1);
NameObs = strcat({'Obs'},num2str((1:NumObs)','%-d'));
iris = dataset({nominal(species),'species'},...
{meas,'SL','SW','PL','PW'},...
'ObsNames',NameObs);

Best Answer

DATASET objects have SET and GET methods defined. Please see the documentation for DATASET SET and GET methods for more information on usage.
Please also note that these methods are similar to the SET and GET methods defined for handle graphics, except for one important difference: to make a change on object OBJ, the object output from a SET command on OBJ must be reassigned to OBJ. If this reassignment is not done, the changes will not be made.
An example of changing the variable names of a dataset object for the IRIS object created above is as follows:
iris = set(iris,'VarNames',{'varname1','varname2','varname3','varname4','varname5'})
As per the reassignment discussion above, if we had executed the following, the changes would not have been made:
set(iris,'VarNames',{'varname1','varname2','varname3','varname4','varname5'})
To change only a single variable name, execute:
varnames = get(iris, 'VarNames');
varnames{3} = 'MyVarName';
iris = set(iris, 'VarNames', varnames);
You can also change a single variable name by explicitly setting this property, i.e.:
iris.Properties.VarNames{1}='newName'