Dev
Dev

Reputation: 339

How can I concatenate vector and cell in MATLAB?

I have matrix F of dimension 5 X 3. For example:

 F= [1 12 13; 
    2 23 24; 
    3 34 35; 
    4 45 46;
    5 56 57]

and I have a label cell of size 1X1 with entry 'v' i.e.

>> label

     label = 

       'v'

and size of F is given by :

>> [m n]=size(F)

     m=

      5

    n =

      3

I want my output to look like:

>> F

    F =

         1    12    13   v
         2    23    24   v
         3    34    35   v
         4    45    46   v
         5    56    57   v

How can I concatenate the cell with the matrix to get this output?

Upvotes: 0

Views: 154

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

As @Jonas described, converting it to cells is the way to go when you want to access the data for further use. However, if you are solely interested in seeing the data on the screen and don't like the brackets this is also an option:

Fcell = [num2str(F) repmat([' ' label{1}],size(F,1),1)]

If your label is actually a char it should work like this:

Fcell = [num2str(F) repmat([' ' label],size(F,1),1)]

Upvotes: 0

Jonas
Jonas

Reputation: 74940

To create an array that contains both numeric and non-numeric data, you need to put everything into a cell array (replace label by {label} in case it isn't a cell array):

Fcell = [ num2cell(F), repmat(label,size(F,1),1)]

You can then access individual numbers/letters using the curly brackets:

Fcell{2,2}

ans =
   23

Upvotes: 4

Related Questions