MATLAB: Export cell array of two strings of different dimension to an excel file

cell arraysexportMATLABstringsxlswrite

I have two cell arrays of strings with different dimension:
A={'Arut';'Babu';'Christ';'Daniel'};
B={'Selvan';'Mahesh';'Colin';'Balaji';'Sathish'};
I need to give an header for these two arrays as 'FIRSTNAME' and 'LASTNAME' in excel file followed by the data of A and B. I have tried to concatenate and write the elements into an excel as follows:
X=[{'FirstName','LastName'};A,B]
xlswrite('A.xls',A{:},B{:})
For which I am getting an error,
Error using horzcat Dimensions of matrices being concatenated are not consistent.
This is due to concatenation of A and B with different length.
Please suggest me how to export these data to an excel with headers

Best Answer

You need to extend your variables so they have equal lengths, which you can do in a way similar to the code below.
A={'Arut';'Babu';'Christ';'Daniel'};
B={'Selvan';'Mahesh';'Colin';'Balaji';'Sathish'};
if length(A)>length(B)
%A is larger, pad B
B{length(A)}=[];
elseif length(A)<length(B)
%B is larger, pad A
A{length(B)}=[];
end
X=[{'FirstName','LastName'};A,B];
xlswrite('A.xls',X)