Cheng
Cheng

Reputation: 289

matlab: Adding square brackets to all individual numerical values within cell arrays

I wish to add a cell array (Ma) into another cell array. However both needs to be of the same format (individual cells in square brackets).

For instance I have an array..

Ma{1,:}.'

ans =

Columns 1 through 8

'83.6'    '85.2'    '91'    '87.9'    '91.8'    '86.3'    '90.6'    '90.2'  

How do i add square brackets to all the numerical values of very individual cells?

Below is what i wish to obtain, it is also a 1x8 cell.

ans =[83.6]    [85.2]    [91]    [87.9]    [91.8]    [86.3]    [90.6]    [90.2]

Upvotes: 0

Views: 751

Answers (2)

Nick
Nick

Reputation: 3203

You don't need to add the square brackets yourself. This just means that it is a numerical value in a cell.

In order to achieve this you should do the following, using both the num2cell as str2double functions:

newM = num2cell(str2double(Ma{1,:}))

Upvotes: 0

Shai
Shai

Reputation: 114966

Your cell values are strings (you can tell by the quote marks ' surrounding the values). You wish to convert them to numerical values ("add square brrckets around them" as you put it).

To convert string to double you can use str2double command:

M = str2double( M{1,:} );

Upvotes: 2

Related Questions