Creating a Table with rows labeled from a matrix and exporting it into an excel file

조회 수: 3 (최근 30일)
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

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 1월 19일
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

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by