Andry
Andry

Reputation: 16875

Converting a cell array into an array

I have a: 1×7 cell array of strings:

arr1 = '0.1' '0.4' '0.0' '0.1' '0.4' '0.0' '2.1'

I need to transform this thing into a proper matrix 1x7 matrix. If I do:

cell2mat(arr1)

I get:

'0.10.40.00.10.40.02.1'

It gives me a single string. What am I doing wrong?

Upvotes: 1

Views: 39

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112759

Use str2double:

>> arr1 = {'0.1' '0.4' '0.0' '0.1' '0.4' '0.0' '2.1'};
>> result = str2double(arr1)
result =
    0.1000    0.4000         0    0.1000    0.4000         0    2.1000

Why didn't your approach work? Because cell2mat simply concatenates the contents of the cells, which are strings, so you get a single string instead of several numbers.

Upvotes: 2

Related Questions