0

Lets say I have following cell array data in Matlab:

>> data = {'first', 1; 'second', 2; 'third', 3}

data = 

    'first'     [1]
    'second'    [2]
    'third'     [3]

Then I want to create a new cell array which has only the first column data. I tried the following but got only the first value instead.

>> column_1 = data{:,1}

column_1 =

first

But what I would like to get as output is:

>> column_1 = {'first';'second';'third'}

column_1 = 

    'first'
    'second'
    'third'

How can I create a sub-cell from first column of data cell array?

1 Answer 1

2

You have to use round parentheses indexing instead of curly braces indexing, like this:

data(:,1)

Output:

ans =
      3×1 cell array
      'first'
      'second'
      'third'

Basically, the purpose of curly braces is to retrieve the underlying content of cells and present a different behavior. For extracting subsets of cells you need to use round parentheses. For more details, refer to this page of the official Matlab documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.