How can I create table from matrix?
Mostrar comentarios más antiguos
Respuestas (2)
Navdha Agarwal
el 20 de Jun. de 2019
Suppose A is the matrix. You can convert it to the table using the following lines of code:
T = array2table(A);
% Default heading for the columns will be A1, A2 and so on.
% You can assign the specific headings to your table in the following manner
T.Properties.VariableNames(1:4) = {'Q25_1','Q25_2','Q25_3','Q25_4'}
5 comentarios
David Logan
el 26 de Jul. de 2019
Slightly simpler, you can write this in one line:
T = array2table(A,'VariableNames',{'Q25_1','Q25_2','Q25_3','Q25_4'});
Gabriela Garcia
el 19 de En. de 2021
This code doesn't work with heterogenous matrix. Do you know how to convert a 12x7 matrix into a table and then into an excel document? I need to title the columns with days of the week and the rows with times from 9:00am to 8:00 pm.
Alan Gil
el 23 de Feb. de 2021
@David Logan, is it possible to also give the rows a name as well on the side? Just like how the columns have a heading above them
Arthur Yip
el 17 de Sept. de 2021
RowNames
A = randi([-1 99], 12, 7); %some sample data
today = dateshift(datetime('now','format', 'hh:mm a'), 'start', 'day');
rt = today + hours(9:20).';
cn = dateshift(today, 'start', 'week') + caldays(0:6);
cn.Format = 'eeee';
dn = string(cn);
T = array2timetable(A, 'rowtimes', rt, 'VariableNames', dn);
T
It was deliberate that I use datetime operations to calculate the names of the days of the week, so that the day names will appear in whatever language is suitable for your area.
Note however, that if your region's "first day of the week" is monday instead of Sunday, then I do not know what order the days will come out as -- and I do not know what order the array happens to have its days in. You might need to circshift(dn) or something like that.
Yao Lionel
el 26 de Sept. de 2023
0 votos
T.Properties.VariableNames={'Name1' , 'Name2', 'Name3', 'Name4'}
Categorías
Más información sobre Numeric Types en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

