What you did is correct. All finite discrete signals have zeros to the left and to the right. Thus, if you want to sum two signals (arrays in MATLAB) you have to zero-pad the shortest one.
A generic solution for this could be written as follows (assuming you have two signals represented by row vectors,
x2 and both start at the same instant of time, i.e. they are both zero until some index $n_0$):
l1 = length(x1);
l2 = length(x2);
if length(x1) < length(x2)
x1 = [x1 zeros(1,l2-l1)];
x2 = [x2 zeros(1,l1-l2)];
Note that if
l1 == l2, the script does nothing, because it inserts a zero amount of zeros in
Sidenote: as Marcus pointed out in the comments, if you want the periodicity to hold then adding a zero is not the solution. You should add as many samples as necessary to make both lengths match, just that not with zeros but with the values the periodic signal would take if it hadn't been truncated.