MATLAB: Do I encounter error using vertcat

vertcat

I want to have mass 9×9 diagonal matrix (mass matrix) and 9×9 stiffness matrix. The code goes like this:
m1= getappdata(handles.mass1_edittxt , 'm1');
m2= getappdata(handles.mass2_edittxt , 'm2');
m3= getappdata(handles.mass3_edittxt , 'm3');
m4= getappdata(handles.mass4_edittxt , 'm4');
m5= getappdata(handles.mass5_edittxt , 'm5');
m6= getappdata(handles.mass6_edittxt , 'm6');
m7= getappdata(handles.mass7_edittxt , 'm7');
m8= getappdata(handles.mass8_edittxt , 'm8');
m9= getappdata(handles.mass9_edittxt , 'm3');
k1= getappdata(handles.stiff1_edittxt , 'k1');
k2= getappdata(handles.stiff2_edittxt , 'k2');
k3= getappdata(handles.stiff4_edittxt , 'k3');
k4= getappdata(handles.stiff4_edittxt , 'k4');
k5= getappdata(handles.stiff4_edittxt , 'k5');
k6= getappdata(handles.stiff4_edittxt , 'k6');
k7= getappdata(handles.stiff4_edittxt , 'k7');
k8= getappdata(handles.stiff4_edittxt , 'k8');
k9= getappdata(handles.stiff4_edittxt , 'k9');
d= getappdata(handles.dampRatio_edittxt,'d');
M=diag([m1 m2 m3 m4 m5 m6 m7 m8 m9]);
k=vertcat([k1 -k1 0 0 0 0 0 0 0;-k1 (k1+k2) -k2 0 0 0 0 0 0;0 -k2 (k2+k3) -k3 0 0 0 0 0;0 0 -k3 (k3+k4) -k4 0 0 0 0;0 0 0 -k4 (k4+k5) -k5 0 0 0;0 0 0 0 -k5 (k5+k6) -k6 0 0;0 0 0 0 0 -k6 (k6+k7) -k7 0;0 0 0 0 0 0 -k7 (k7+k8) -k8;0 0 0 0 0 0 0 -k8 (k8+k9)]);
Can anyone help me why this code shows error as follows:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in finally>calculatemodeshape_button_Callback (line 336)
k=vertcat([k1 -k1 0 0 0 0 0 0 0;-k1 (k1+k2) -k2 0 0 0 0 0 0;0 -k2 (k2+k3) -k3 0 0 0 0 0;0 0 -k3 (k3+k4) -k4 0 0 0 0;0 0 0
-k4 (k4+k5) -k5 0 0 0;0 0 0 0 -k5 (k5+k6) -k6 0 0;0 0 0 0 0 -k6 (k6+k7) -k7 0;0 0 0 0 0 0 -k7 (k7+k8) -k8;0 0 0 0 0 0 0 -k8
(k8+k9)]);

Best Answer

If you getappdata() of a variable that has not been stored into the appdata, then there will not be an error message, and instead [] will be returned. When you [] together the list of variables and that empty variable is substituted in, vertcat does not substitute 0 and does not leave a hole: it assumes you might have wanted the array to be empty and proceed to try to put all the rows together (but they are different sizes and it cannot do so.)
By the way, you do not need to call vertcat() because the [] operator already implies vertcat and horzcat as needed.