xlswrite is not recommended, what should I use instead?

I have eleven 12x8 matrices composed of 1s and 0s. I want to export each matrix into its own excel file as a table with the rows labeled with times and the columns labeled with weekdays. I want to use an efficient code and possibly a loop to prevent typing over and over again. Each Matrix is saved as as a variable, like 'A53'. Of the eleven matrices, ten are have consecutive counting titles, like 'A51', 'A52', 'A53'...'A60'. Is it possible to use a loop to load these into separate excel sheets, as described above?

Réponses (1)

Star Strider
Star Strider le 19 Jan 2021

1 vote

Since you have R2020a, first, create a table using your data, then use writetable to write it to an Excel file (or other options).

8 commentaires

Would you happen to know if I could use a loop to get through all of the matrices?
I would use a loop, yes.
It would be straightforward to save them to different files. See: Process a Sequence of Files for an illustration.
T = table(A51, A52, A53... list them all)
Now T.Properties.VariableNames{K} tells you the original variable name and T{:,K} gives you the content of the Kth of them and you can proceed to write in appropriate format.
This does involve explicitly naming all of the variables once. The options that do not require that are not recommended.
Thank you so much for your help! I am very new to MATLAB (in case you couldn't tell). Would I write the code that you described like this?
T=table(A51,A52,A53,A54,A55,A56,A57,A58,A59,A60)
T.9:00am,10:00am,11:00am,12:00pm,1:00pm,2:00pm,3:00pm,4:00pm,5:00pm,6:00pm,7:00pm,8:00pm,9:00pm.StudentID
T{:,K} = Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
I apologize for my confusion. I do not know what information goes where
rownames = {'9:00am','10:00am','11:00am','12:00pm','1:00pm','2:00pm','3:00pm','4:00pm','5:00pm','6:00pm','7:00pm','8:00pm','9:00pm'}
T = table(A51,A52,A53,A54,A55,A56,A57,A58,A59,A60);
names = T.Properties.VariableNames;
nn = length(names);
for K = 1 : nn
arr = T{:,K};
filename = [names{K} '.xlsx'];
tab = array2table(arr, 'VariableNames', {'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'}, 'rownames', rownames);
writetable(tab, filename);
end
I do not know how the StudentID is intended to fit in.
Note: You said that your arrays were 12 x 8, but you have 13 row names and 7 column names; that needs to be cleared up.
Thank you for your help! Do you think that there is a way to have the new excel documents open when I run the program?
Open in which program?
While I run the script that is supposed to write the matrices into their own excel sheets, is there another function to add that will have the new excel documents open?

Connectez-vous pour commenter.

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by