필터 지우기
필터 지우기

How to write data to an excel file?

조회 수: 22 (최근 30일)
VR
VR 2019년 5월 1일
댓글: Star Strider 2019년 5월 1일
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)
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 5월 1일
In the majority of cases you should create the variables as with tables columns not rows

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2019년 5월 1일
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.
  댓글 수: 2
VR
VR 2019년 5월 1일
Thanks for your suggestions. I tried this and got an error:
data1 = 1:1:10;
data2 = 10:10:100;
data =[data1(:) data2(:)]; % to make them column vectors
T1 = table(data);
T1.Properties.VariableNames = {'Name' 'Age'}
writetable(T1, 'Table.xls', 'Sheet', 'MySheetName')
Error is: The VariableNames property must contain one name for each variable in the table.
Star Strider
Star Strider 2019년 5월 1일
As always, my pleasure.
Create ‘T1’ as:
T1 = table(data1(:),data2(:), 'VariableNames',{'Name' 'Age'});
That works.
Concatenating the variables as one variable is permitted, although if you want to give them different names, you need to have the number of columns equal the number of variable names.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by