Effacer les filtres
Effacer les filtres

How to create multiple excel sheets in Matlab?

151 vues (au cours des 30 derniers jours)
Milad Zarei
Milad Zarei le 12 Sep 2017
Commenté : Milad Zarei le 12 Sep 2017
I have 2 tables, and I am trying to use writetable commend to create Excel sheet. When I try to have more than one sheet, it would not work. It just overwrites the first sheet. This is piece of script I use: By the way, I am running Matlab on a Macbook.
A = randn(5,5);
Tab1 = table(A,'VariableNames',{'X1' 'X2' 'X3' 'X4' 'X5'});
B = rand(5,1);
Tab2 = table(B,'VariableNames',{'Y1'});
sheet = 1;
writetable(Tab1,filename,'sheet',1,'Range','A1')
sheet = 2;
writetable(Tab2,filename,'sheet',1,'Range','A1')
Can you give me some tips? Thanks.

Réponse acceptée

OCDER
OCDER le 12 Sep 2017
Looks like you're overwriting sheet 1 in the last line of your script.
sheet = 1;
writetable(Tab1,filename,'sheet',1,'Range','A1')
sheet = 2;
writetable(Tab2,filename,'sheet',1,'Range','A1') %This is overwriting sheet 1
Try this:
sheet = 1;
writetable(Tab1,filename,'sheet',sheet,'Range','A1')
sheet = 2;
writetable(Tab2,filename,'sheet',sheet,'Range','A1')
  1 commentaire
OCDER
OCDER le 12 Sep 2017
Modifié(e) : OCDER le 12 Sep 2017
Haha, looks like 3 of us submitted an answer at the same time. Matlab Answer should provide a notice when hitting "Submit" button - "There has been new activity / answer to this question. Check?"

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 12 Sep 2017
Since sheet is a variable that you set but never use, how about you use it???
writetable(Tab2, filename, 'sheet', sheet, 'Range', 'A1')

KL
KL le 12 Sep 2017
Modifié(e) : KL le 12 Sep 2017
sheet1 = 1;
writetable(Tab1,filename,'sheet',sheet1 ,'Range','A1')
sheet2 = 2;
writetable(Tab2,filename,'sheet',sheet2,'Range','A1')
You should just use that sheet variable in the command. You missed it pretty close.
  1 commentaire
Milad Zarei
Milad Zarei le 12 Sep 2017
I accepted Donald's respond since he answered earlier. Thanks :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by