How to add row and column labels to matrix?
Show older comments
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
More Answers (2)
KSSV
on 24 May 2018
row = {'head1' ; 'head2' ; 'head3' } ;
head1 = rand(3,1) ;
head2 = rand(3,1) ;
head3 = rand(3,1) ;
T = table(row,head1,head2,head3)
1 Comment
Akira Agata
on 24 May 2018
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
chocho
on 24 May 2018
KSSV
on 24 May 2018
To remove duplicates, use unique
Akira Agata
on 24 May 2018
Edited: Akira Agata
on 24 May 2018
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
chocho
on 24 May 2018
Categories
Find more on Axis Labels 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!

