MATLAB: How to write data to an excel file

excelMATLABtable

In the process of learning how to write data to an excel file, I learnt that I should first write it as a table. So I gave it a try but what my code does is write two row vectors as one long row vector. I wanted to write two variables as two separate rows. Here is my attempt:
(My final goal is to save my workspace data to an excel file.)
data1 = 1:1:10;
data2 = 10:10:100;
data =[data1; data2];
T1 = table(data);
writetable(T1)

Best Answer

The table function prefers column vectors. I would save them as columns:
data1 = 1:1:10;
data2 = 10:10:100;
data =[data1(:), data2(:)];
T1 = table(data);
You can convert them back to row vectors later, if necessary, when you read the file. It is best to provide the path name as well as the file name to writetable. This way, you know where it is and what its name is. If you want to save it as an Excel file, be sure that you designate the '.xlsx' extension.