How to add row and column labels to matrix?

Hello guys, I have a matrix of size 135*135 and a cell array of type strings size 135*1
I want to add this cell array in the row and columns of this matrix.
Example_inputs
Expected_outputs

 Accepted Answer

Duplicate row names and Variables are not supported in MATLAB, consider adding your labels as an extra column of the table. For example
x = rand(4);
label = compose('head%d', [1 1 2 3])';
cellArray = [[{' '}; label]'; label num2cell(x)];
T = cell2table(cellArray);
T =
5×5 table
cellArray1 cellArray2 cellArray3 cellArray4 cellArray5
__________ __________ __________ __________ __________
' ' 'head1' 'head1' 'head2' 'head3'
'head1' [0.6518] [0.7975] [0.3759] [0.4024]
'head1' [0.1314] [0.7482] [0.8932] [0.6812]
'head2' [0.7244] [0.0342] [0.1829] [0.7504]
'head3' [0.6333] [0.5028] [0.1158] [0.7177]

13 Comments

@Ameer Hamza, hello I have 135 heads
plz, can you generalize this code?
label = compose('head%d', [1 1 2 3])';
this line how can be updated for 135 heads??
Do you mean this
label = compose('head%d', 1:135)';
@Ameer Hamza but my labels are not named head, i have 135 names.
PLz, help I need to do this job thanks!
Yes, this code will work with any label vector in general. I just wrote this label vector as an example, if you have your own label vector. It will work fine. For example, you can define label vector as
label = {'label1', 'secondLabel', 'labelthird', 'fourth'}
the label can have different names in it. You don't need to use compose().
@Ameer Hamza Thanks a lot it works very well!
Could i ask you another and last question plz?
You are welcome. Sure, you can ask.
chocho
chocho on 24 May 2018
Edited: chocho on 24 May 2018
Thanks, Now i want to write this matrix into a text file of a format like every row with its every corresponding column and their corresponding cell value.
I attached a small example to just give you some hints about the format i want.
If it is a table then you can use writetable().
How can i use writeable from this matrix? I can open a new thread if you want and you can give a response
writeTable(table_name, 'filename.txt');
You can check the class of variable using
class(table_name);
@Ameer Hamza, you didn't get my question. Suppose this is my inputs
Wanted_outputs:
In that case, you don't need to make the table first according to the code in the answer. You can make it like this
x = rand(4);
x = x';
label = compose('head%d', 1:4)';
labelColumnsIndex = fliplr( combvec((1:length(label)), (1:length(label)))' );
labelColumns = label(labelColumnsIndex);
T = [array2table(labelColumns) array2table(x(:))];
chocho
chocho on 24 May 2018
Edited: chocho on 24 May 2018
@Ameer Hamza Thank you too too much, well done!

Sign in to comment.

More Answers (2)

row = {'head1' ; 'head2' ; 'head3' } ;
head1 = rand(3,1) ;
head2 = rand(3,1) ;
head3 = rand(3,1) ;
T = table(row,head1,head2,head3)

1 Comment

chocho
chocho on 24 May 2018
Edited: chocho on 24 May 2018
@KSSV I have 135 heads How to generalize this code? and my matrix is of size 135*135 too

Sign in to comment.

OK. Then, how about the following? I believe it will be applicable to 135-by-135.
% Sample data
x = rand(4);
label = {'head1';'head2';'head3';'head4'};
% Store them in table
T = array2table(x,'RowNames',label,'VariableNames',label);
The result looks like:
>> T
T =
4×4 table
head1 head2 head3 head4
_______ ________ _______ ________
head1 0.42176 0.65574 0.67874 0.65548
head2 0.91574 0.035712 0.75774 0.17119
head3 0.79221 0.84913 0.74313 0.70605
head4 0.95949 0.93399 0.39223 0.031833

4 Comments

I tried this code on my data and it shows me this error Duplicate row name: 'head2'.
it seems that i have some duplicated heads but i need them and i can't delete them. How to avoid this problem plz
like in my cells there are some repeated heads like this 'ptr04015:Rap1 signaling pathway' 'ptr04015:Rap1 signaling pathway'
To remove duplicates, use unique
Maybe you need not only to remove duplication, but also to remove/replace special characters from row name. Following would be one possible solution.
% Sample data (label contains duplication and special characters)
x = rand(4);
label = {...
'head1';...
'head2';...
'ptr04015:Rap1 signaling pathway';...
'ptr04015:Rap1 signaling pathway'};
% Replace special character to '_'
label = regexprep(label,'\W','_');
% To avoid duplication
suffix = arrayfun(@(k) {sprintf('Ver%03d_',k)},(1:N)');
label = strcat(suffix,label);
T = array2table(x,'RowNames',label,'VariableNames',label);
The output is:
>> T
T =
4×4 table
Ver001_head1 Ver002_head2 Ver003_ptr04015_Rap1_signaling_pathway Ver004_ptr04015_Rap1_signaling_pathway
____________ ____________ ______________________________________ ______________________________________
Ver001_head1 0.8909 0.14929 0.81428 0.1966
Ver002_head2 0.95929 0.25751 0.24352 0.25108
Ver003_ptr04015_Rap1_signaling_pathway 0.54722 0.84072 0.92926 0.61604
Ver004_ptr04015_Rap1_signaling_pathway 0.13862 0.25428 0.34998 0.47329
suffix = arrayfun(@(k) {sprintf('Ver%03d_',k)},(1:N)');
N is not defined ? i have change by k but still have problem
it says:
'Ver064_ptr04550_Signaling_pathways_regulating_pluripotency_of_stem_cells' is not a valid variable name.
I can share the data if you want?

Sign in to comment.

Asked:

on 24 May 2018

Edited:

on 24 May 2018

Community Treasure Hunt

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

Start Hunting!