MATLAB: How to write multiple lines of data within the same cell in a Microsoft Excel worksheet using MATLAB 7.8 (R2009a)

MATLABxlsreadxlswrite

I am writing a MATLAB script to write data from MATLAB into Excel using XLSWRITE. I have multiple numbers to write. I want to write them in the same cell.
It is possible to enter numbers in multiple lines within the same cell directly in Excel using Alt+Enter. I want to simulate this behavior in a cell using XLSWRITE. How can i acheive this?

Best Answer

This can be done by introducing the ASCII value for 'New Line', which is 10, in between the numbers. This is illustrated by the example below:
%Initialize three numbers x1,x2,x3
x1 = 100; x2 = 200;x3=300;
%Create a cell that contains all the three numbers separated by 'New line' character
% char(10) introduces a new line
x = {[num2str(x1) char(10) num2str(x2) char(10) num2str(x3)]};
%Write the cell to an Excel sheet
xlswrite('Book1.xls',x,'Sheet1')
Note: Starting with R2016b the newline function was introduced which is the equivalent to char(10). It is recommend to use newline instead of char(10) in 16b and newer.