Converts a numeric variable or term of this to a cell array of strings. Usage: f = var2fac( x [, str] ); x = n x 1 numeric vector or term of this. str = either a single string, or a 1 x k cell array of strings of names for the k unique values of x in ascending order. If str is absent, then str='x'. f = n x 1 cell array of strings. If str is a single string, then the strings are 'str1', 'str2', .... 'strk'. Examples: gender=var2fac([repmat(1,1,64) repmat(2,1,44)],{'male'; 'female'}) subj=var2fac(repmat(1:27,4,1),'subj')
0001 function f = var2fac( x, str ); 0002 0003 %Converts a numeric variable or term of this to a cell array of strings. 0004 % 0005 % Usage: f = var2fac( x [, str] ); 0006 % 0007 % x = n x 1 numeric vector or term of this. 0008 % str = either a single string, or a 1 x k cell array of strings of names 0009 % for the k unique values of x in ascending order. If str is absent, 0010 % then str='x'. 0011 % 0012 % f = n x 1 cell array of strings. If str is a single string, then the 0013 % strings are 'str1', 'str2', .... 'strk'. 0014 % 0015 % Examples: 0016 % gender=var2fac([repmat(1,1,64) repmat(2,1,44)],{'male'; 'female'}) 0017 % subj=var2fac(repmat(1:27,4,1),'subj') 0018 0019 x=double(x); 0020 n=numel(x); 0021 if nargin<2 0022 str=inputname(1); 0023 end 0024 if ischar(str) 0025 for i=1:n 0026 f{i}=[str num2str(x(i))]; 0027 end 0028 f=f'; 0029 else 0030 [u,i,j]=unique(x); 0031 f=str(j); 0032 end 0033