1

I have a cell data type matrix containing a header and a large number of rows.

sample data:

set         press       dp
32.7045     17.805965   123.75047
32.690094   17.80584    123.74992
32.6232     17.815094   123.790115

I am trying to find the index of a specific column using the strcmp command to search through the all the data.

dpCol = strcmp([data{:}], 'dp')

This always returns

dpCol =

     0

Am I using the data cell type wrong or something? Thank you!

2
  • 2
    To understand why your code is not working, take a look at the intermediate result [data{:}]. This is definitely not what you want. Commented Jan 12, 2016 at 21:08
  • Tried other options such as [date{1,:}] but that didn't help... Commented Jan 12, 2016 at 21:49

1 Answer 1

2

Try using cell notation to yield just the 1st row, EG:

data(1,:) = {'set','press','dp'}

instead of unpacking* the entire cell array since strcmp can operate on cell arrays.

>>> data = {'set'         'press'       'dp'
32.7045     17.805965   123.75047
32.690094   17.80584    123.74992
32.6232     17.815094   123.790115}

data = 

    'set'        'press'      'dp'      
    [32.7045]    [17.8060]    [123.7505]
    [32.6901]    [17.8058]    [123.7499]
    [32.6232]    [17.8151]    [123.7901]

>>> col_idx = strcmp(data(1,:),'dp')

col_idx=

     0     0     1

Then return the dp using the logical indices and cell2mat...

>>> dp = cell2mat(data(2:end,col_idx))
dp =

  123.7505
  123.7499
  123.7901

or unpack* and concatenate the comma separated list

>>> dp = [data{2:end,col_idx}]

dp =

  123.7505  123.7499  123.7901

As an alternative try cell2struct.

>>> datastruct = cell2struct(data(2:end,:),data(1,:),2)
datastruct = 

3x1 struct array with fields:

    set
    press
    dp

Then dp is ...

>>> dp = [datastruct.dp]

dp =

  123.7505  123.7499  123.7901

* Using the colon operator inside curly braces unpacks an cell array into a comma separated list. Using square brackets horizontally concatenates the comma separated list which returns a character array set pressdp{{{ since the first item in the list is a character array. The garbage characters between and after 'set', 'press' and 'dp' are caused by reading the doubles as char. IE: char(32.7045) is the ASCII equivalent of whitespace. The arrays always get unpacked as column.

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

2 Comments

Thank you Mark, does exactly what I needed. Another issue did arise giving me the following error Error using cat Dimensions of matrices being concatenated are not consistent. Error in cell2mat (line 83) m{n} = cat(1,c{:,n}); Error in OpenFile (line 38) dp = cell2mat(data(2:end,col_idx)) Tried replacing col_index with the column number, doesn't help either.
The alternative option does return results but they are all in the same cell. All the results are concatenated one after the other.

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.