Creating a Table with rows labeled from a matrix and exporting it into an excel file
3 views (last 30 days)
Show older comments
Gabriela Garcia
on 19 Jan 2021
Commented: Gabriela Garcia
on 19 Jan 2021
My code is combersome and complicated. I want to read the matrix and its specific values (as indicated in lines 3 and 4) then convert it into a table with labels. I want the columns to be labeled the days of the week, starting with Sunday, and the rows labeled times from 9:00am to 8:00pm. Once I have that table created, I want to export it into an excel file. I would like the new excel file to open when I run the script, but that is not a dire necessity. Please help and explain if you can. I really appreciate it!
clear;
M51 = readmatrix('a51.xlsx','Sheet','Sheet1','Range','B2:H13');
M51(8:12,1)=51;
M51(5,2)=51;
z51=num2cell(M51);
time51={'hours';'9:00';'10:00';'11:00';'12 noon';'1:00 p.m.';'2:00';'3:00';'4:00';'5:00';'6:00';'7:00';'8:00'};
a51={'Sunday';[];[];[];[];[];[];[];[];[];[];[];[]};
b51={'Monday';[];[];[];[];[];[];[];[];[];[];[];[]};
c51={'Tuesday';[];[];[];[];[];[];[];[];[];[];[];[]};
d51={'Wednesday';[];[];[];[];[];[];[];[];[];[];[];[]};
e51={'Thursday';[];[];[];[];[];[];[];[];[];[];[];[]};
f51={'Friday';[];[];[];[];[];[];[];[];[];[];[];[]};
g51={'Saturday';[];[];[];[];[];[];[];[];[];[];[];[]};
K51 = table(time51,a51,b51,c51,d51,e51,f51,g51);
Table51 = table2array(K51);
Table51(1:12,2:8)=z51;
writecell(Table51,'51Library.xlsx','Sheet',1); %creates the excel file for student 1 during the semester
0 Comments
Accepted Answer
Cris LaPierre
on 19 Jan 2021
If you want row times, you might be interested in created a timetable instead of a table.
We don't have your spreadsheet, but the resulting code might look something like this.
varNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
Hours = duration('9:00',"Format","hh:mm")+hours(0:11)';
TTable51 = array2timetable(zeros(12,7),'RowTimes',Hours,'VariableNames',varNames);
writetimetable(TTable51,'51Library.xlsx','Sheet',1);
3 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!