Search code examples
matlabcsvcustomizationlabeldendrogram

How to put Label in a dendogram in Matlab using function dendrogram(tree, name, value)?


I have tried the following link (unfortunately, didn't work): Associated Labels in a dendrogram plot - MATLAB Instead of Station Ids, mine will be PDBId.

My Problem: I am creating a dendrogram from a csv file "similarity_nogrp.csv" :

PDBId	0	        1	        2           3	            4	    
1A06	1	        0.05344457	0.439285476	0.46664877	0.40868216	
1B6C	0.05344457	1	        0.03371103	0.029899324	0.033972369
1BO1	0.439285476	0.03371103	1	        0.5579095	0.488785068	
1CDK	0.46664877	0.029899324	0.5579095	1	        0.50682912	
1CJA	0.40868216	0.033972369	0.488785068	0.50682912	1	
1CSN	0.490366809	0.047467331	0.50842029	0.533638473	0.465180315
1E8X	0.036246998	0.002009194	0.057903016	0.066882369	0.058359239	
Here, in first line,

PDBId is row Id,

0 1 2 3 4 are column numbers,

I want to label the leaf nodes as per PDBId, but as I am reading the csv file from 2nd column (only numbers, leaving the PDBIds), later while setting 'Labels' as 'PDBId' in dendrogram(), I am getting error:

Here is my code:

filename = 'D:\\matlab codes\\similarity_nogrp.csv'
X = csvread(filename,1,1) 
Z = linkage(X,'average')
C = cluster(Z, 'maxclust', 3)
H = dendrogram(Z,'Orientation','left','Labels',filename.PDBId)

The error for last line is:

??? Attempt to reference field of non-structure array.
Please provide me a way such that I can use the PDBId as my labels for the leaf nodes. Thanks in advance.


Solution

  • So, your problem is that you are trying to access a struct field of filename, but filename is a character array, not a struct. Also, readcsv can only read numeric values, so it won't get the labels for you anyway.

    You can use readtable instead to get the row and column names, and then read the row names off of the table. Here's the code I used:

    filename = 'D:\\matlab codes\\similarity_nogrp.csv';
    T = tableread(filename,'ReadVariableNames', true, 'ReadRowNames', true)
    X = T{:,:}; % Get the data from the table without row/col names
    Z = linkage(X,'average')
    C = cluster(Z, 'maxclust', 3)
    H = dendrogram(Z,'Orientation','left','Labels',T.Properties.RowNames)
    

    And the results I got: Results