MATLAB Answers


Concatenate cells: making column and row headers

Asked by Mich
on 17 Dec 2012
I have a column and a row like this:
col={'cat1'; 'cat2'; 'cat3'}
row={'mouse1' 'mouse2' 'mouse3'}
I would like to make these as column and row headers (eventually for a table) like this such that my table looks like this:
I've attempted to use cat such that:
but it results in an error the dimensions are not consistent..
Thank you so much for your help and please let me know if I am not clear.


Your image is invisible
Oh! that is weird, here is the link: I'm sorry!

Sign in to comment.



3 Answers

Answer by Pedro Villena on 17 Dec 2012
 Accepted Answer

table(2:numel(row)+1) = row;
table(2:numel(col)+1,1) = col;

  1 Comment

That's it! The way that gave me the exact result was:
Thank you so much!! And to all the helpers, you are awesome!

Sign in to comment.

Answer by Honglei Chen
on 17 Dec 2012

table(2:4) = row;
table(2:4,1) = col;


Unfortunately i get a dimension mismatch error as well. I tried the method under Azzi's comment because with my real dataset I would not know the if it actually ended on the 4th row. Perhaps I am looking for something more like:
But I get a dimension mismatch.. Thank you for your response!!
The code below works fine for me. If you get an error, you may need to make sure that your variable, table, is not defined previously with other dimensions. If that still didn't work out, please post your example with the error message.
>> col = {'a','b','c'}
col =
'a' 'b' 'c'
>> row = {'d','e','f'}
row =
'd' 'e' 'f'
>> table(2:numel(row)+1) = row
table =
[] 'd' 'e' 'f'
>> table(2:numel(col)+1,1) = col
table =
[] 'd' 'e' 'f'
'a' [] [] []
'b' [] [] []
'c' [] [] []

Sign in to comment.

Answer by Matt Fig
on 17 Dec 2012
Edited by Matt Fig
on 17 Dec 2012

row={'cat1'; 'cat2'; 'cat3'};
col={'mouse1' 'mouse2' 'mouse3'};
t = uitable('Data',[],...
'Position',[20 20 360 100])


Sign in to comment.