Question

This is what part of my data it looks like: It's actually a 1x7 cell and this is part of PM25{1,1} The data can be found here: https://www.dropbox.com/sh/li3hh1nvt11vok5/4YGfwStQlo Variable is PM25

'42.493056'    '-92.343889'    '19-013-0008'    [733043]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733044]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733045]    '3.6' 
'42.493056'    '-92.343889'    '19-013-0008'    [733046]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733047]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733048]    '10'  
'42.493056'    '-92.343889'    '19-013-0008'    [733049]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733050]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733051]    '5.8' 
'42.493056'    '-92.343889'    '19-013-0008'    [733052]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733053]    [ NaN]
'42.493056'    '-92.343889'    '19-013-0008'    [733054]    '7.7' 

Is it possible to convert this cell array to a matrix? I tried str2double and cell2mat. I want to sort it according to the 5th column (I tried sortrows and the FileExchange sortcell.m), but it's telling me that the data type is not the same for column 5.

I tried converting the whole matrix, but that doesn't work the way I want it to

for i = 1:7
    str2double(PM25{i}) % Turns the 3rd and 4th columns to NaN.
    cell2mat(PM25{i}) % Gives this error: `Error using cell2mat. All contents of the input cell array must be of the same data type.`
end

How can I overcome this?

If I can't convert it, I want to at least be able to convert the last column above to one data type for all of the 1x7 cells (str2double only converts one cell) so that I can use sortcell. Or else, I have to convert the 5th column to double, use sortrow to sort it descending without considering the NaN, and then try to keep track of the index for all the other columns through everything I do to the 5th column (and I don't know how to do that for all the steps).

Was it helpful?

Solution

I would sort by the 5th row without using sortrows. Then you only have to convert the 5th row to double:

%sorted indices
[~,ix]=sort(str2double(M(:,5)));
%get mask of nan-rows to be ignored
nanmask=isnan(str2double(M(ix,5)))
%get non-nan indices in reverse order
ix=flipdim(ix(~nanmask),1)
%sort
sortedM=M(ix,:);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top