Search code examples
matlabmatrixdoublecell

MATLAB Populate matrix with elements from a cell array


I have a matrix and I want to put into the third column of the matrix, elements from a cell array. How can I do this?

Here is an example of what I mean. This is the matrix (E):

43.4350000000000    -88.5277780000000   NaN 733144
43.4350000000000    -88.5277780000000   NaN 733146
43.4350000000000    -88.5277780000000   NaN 733148
43.4350000000000    -88.5277780000000   NaN 733150

I want to take the NaN column (column 3) and put into it, the elements of a cell array (uID) The cell array looks like this:

'027-0007'
'079-0026'
'119-8001'
'133-0027'

I used this code:

E(:,3) = reshape(repmat(uID',length(all_dates),1),[],1)

to replicate each line of uID a certain number of times and then reshape it into a column so that's it's the same size as a column of E.

However, when I run it now, the fact that E is a matrix and uID is a cell causes MATLAB to tell me thatConversion to double from cell is not possible. The part to the right of the = works fine. It's the placing the cell elements into E that's causing the problem.


Solution

  • Instead of inserting the data into a normal matrix, you can insert it into another cell

      Ecell=num2cell(E); 
      Ecell(:,3)=uID;