0
votes

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.

2
The cell contains strings, whereas the destination matrix must contain numbers. How do you want to transform for example '027-0007' into a number? Would that be 20? Or 270007? Or...? - Luis Mendo
Also, why do you need to repmat the cell array? Its size seems to fit the column size in your example - Luis Mendo
027-0007 is not a number. It's a siteID. That's why I'm having this problem. My example is only a small part of a much larger array which is why I'm using repmat. The actual array is 376x4. - SugaKookie
You can only fill a matrix with numbers, not with siteID's (strings) - Luis Mendo
This is part of a larger question (stackoverflow.com/questions/21390103/…). Could you maybe take a look at that one then? If I can't fill a matrix with strings, I can't check for consistency across dates and SiteIDs to fill in data or add in NaN. The answer given for that question has a good approach, but runs into this problem. - SugaKookie

2 Answers

0
votes

The contents of your cell array are not numeric and therefore cannot be inserted into a numeric matrix. You can use str2double to convert strings cell arrays to numeric arrays like in the following

 >> str2double({'3','17.5'})

ans =

    3.0000   17.5000

but that's only when the string contents of the cell represent actual numbers, which doesn't seem to be true in your case.

0
votes

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

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