Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Convert cell array to matrix?

Subject: Convert cell array to matrix?

From: Roberto

Date: 24 Nov, 2010 14:29:05

Message: 1 of 3

Hi everyone, I'm trying to convert a cell array like the following into a matrix:

data =
    '99' '016' '256' '6' '29' '120'
    '00' '172' '///' '4' '//' '125'
    '92' '851' '198' '8' '37' '///'
    '85' '576' '156' '/ '49' '150'

However I haven't been able to do it because the cell contains non-integers "/", "//", "///" indicating missing data. I tried to change the "/"s to NaNs with no luck. Anyone suggests how to convert the "/" to NaNs and then to a matrix? Or any other way?

Thanks,

Roberto.

Subject: Convert cell array to matrix?

From: Aurelien Queffurust

Date: 24 Nov, 2010 15:05:05

Message: 2 of 3

"Roberto " <r.fernandez-bilbao@uea.ac.uk> wrote in message <icj7fh$d9n$1@fred.mathworks.com>...
> Hi everyone, I'm trying to convert a cell array like the following into a matrix:
>
> data =
> '99' '016' '256' '6' '29' '120'
> '00' '172' '///' '4' '//' '125'
> '92' '851' '198' '8' '37' '///'
> '85' '576' '156' '/ '49' '150'
>
> However I haven't been able to do it because the cell contains non-integers "/", "//", "///" indicating missing data. I tried to change the "/"s to NaNs with no luck. Anyone suggests how to convert the "/" to NaNs and then to a matrix? Or any other way?
>
> Thanks,
>
> Roberto.


 >>data ={ '99' '016' '256' '6' '29' '120'
    '00' '172' '///' '4' '//' '125'
    '92' '851' '198' '8' '37' '///'
    '85' '576' '156' '/' '49' '150'}

>>my_matrix = str2double(data)

my_matrix =

    99 16 256 6 29 120
     0 172 NaN 4 NaN 125
    92 851 198 8 37 NaN
    85 576 156 NaN 49 150

Subject: Convert cell array to matrix?

From: John

Date: 24 Nov, 2010 15:13:03

Message: 3 of 3

"Roberto " <r.fernandez-bilbao@uea.ac.uk> wrote in message <icj7fh$d9n$1@fred.mathworks.com>...
> Hi everyone, I'm trying to convert a cell array like the following into a matrix:
>
> data =
> '99' '016' '256' '6' '29' '120'
> '00' '172' '///' '4' '//' '125'
> '92' '851' '198' '8' '37' '///'
> '85' '576' '156' '/ '49' '150'
>
> However I haven't been able to do it because the cell contains non-integers "/", "//", "///" indicating missing data. I tried to change the "/"s to NaNs with no luck. Anyone suggests how to convert the "/" to NaNs and then to a matrix? Or any other way?
>
> Thanks,
>
> Roberto.

Hi,

I can offer you only a long way solution, with 2 for loops. (a is your cell array)

sizeA = size(a);

for i = 1:sizeA(1)
    for j = 1:sizeA(2)
        if ~ isnumeric(a{i,j})
            a{i,j} = nan;
        end
    end
end

a = cell2mat(a);

Regards,

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us