How can I create table from matrix?

I have a matrix like this and I want to create a table with a specific heading of each column.
It should be like this below image:
How can it be done?please help anyone

2 件のコメント

Stephen23
Stephen23 2018 年 3 月 29 日
Jhon Gray
Jhon Gray 2018 年 3 月 29 日
thanks a lot

サインインしてコメントする。

回答 (2 件)

Navdha Agarwal
Navdha Agarwal 2019 年 6 月 20 日

9 投票

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 件のコメント

David Logan
David Logan 2019 年 7 月 26 日
Slightly simpler, you can write this in one line:
T = array2table(A,'VariableNames',{'Q25_1','Q25_2','Q25_3','Q25_4'});
Gabriela Garcia
Gabriela Garcia 2021 年 1 月 19 日
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
Alan Gil 2021 年 2 月 23 日
@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
Arthur Yip 2021 年 9 月 17 日
RowNames
Walter Roberson
Walter Roberson 2023 年 9 月 26 日
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
T = 12×7 timetable
Time Sunday Monday Tuesday Wednesday Thursday Friday Saturday ________ ______ ______ _______ _________ ________ ______ ________ 09:00 AM 9 52 74 56 42 21 66 10:00 AM 0 83 24 75 3 59 91 11:00 AM 31 53 80 14 53 66 4 12:00 PM 69 22 72 9 16 67 9 01:00 PM 23 99 89 66 19 46 91 02:00 PM 56 36 56 69 18 72 44 03:00 PM 20 62 60 1 20 35 4 04:00 PM 89 9 63 42 14 32 63 05:00 PM 8 78 69 65 70 12 4 06:00 PM 17 98 17 56 98 69 52 07:00 PM 17 22 36 63 97 4 58 08:00 PM 93 61 90 10 66 58 71
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
Yao Lionel 2023 年 9 月 26 日

0 投票

T.Properties.VariableNames={'Name1' , 'Name2', 'Name3', 'Name4'}

カテゴリ

ヘルプ センター および File ExchangeNumeric Types についてさらに検索

質問済み:

2018 年 3 月 29 日

コメント済み:

2023 年 9 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by