MATLAB: Labelling matrix dynamically regardless of its size

column namelabel;matrixrow name

Hello,
I want to label an n-by-m matrix dynamically using horzcat. I'm using the following code:
X=[];
for i=1:size(tableau,1)
X=[X strcat('x', num2str(i))];
end
Y=[];
for j=1:size(tableau,2)
Y=[Y strcat('y', num2str(j))];
end
t=horzcat({''; strcat('', X) ;'M'}, ...
vertcat({ strcat('', Y),'N'},...
num2cell(tableau)));
However I'm getting an error "??? Error using ==> vertcat CAT arguments dimensions are not consistent."
The objective is to obtain the following output regardless of the matrix size:
'' 'y1' 'y2' 'y3'
'x1' 5 5 3
'x3' 3 1 3
'x3' 7 6 4
Thanks for your help.

Best Answer

>> M = [5,5,3;3,1,3;7,6,4];
>> C = num2cell(M);
>> C(2:end+1,2:end+1) = C;
>> C{1,1} = '';
>> C(2:end,1) = strcat('x',cellstr(num2str((1:size(M,1)).')));
>> C(1,2:end) = strcat('y',cellstr(num2str((1:size(M,2)).')));
>> C
C =
'' 'y1' 'y2' 'y3'
'x1' [ 5] [ 5] [ 3]
'x2' [ 3] [ 1] [ 3]
'x3' [ 7] [ 6] [ 4]
Or for a much better solution, use the new table variable class, which includes row and column names: