Orangeblue
Orangeblue

Reputation: 229

Access elements from cells arrays in matlab

Say I have a cell arrays (named as 'data') as below:

'k0'    'k1'    'Agg'   'RH'    'AQ'    'fr'    'frac'  'Cel'   'R_A'   'Tot'   'AO'
1.15    1.1574  1.50    0.99    0.090   3.45    1.10    1   11x2 double     11x2 double  11x2 double

How do I get the last element or any element that I desire from the columns

'frac'  'Cel'   'R_A'
11x2 double 11x2 double  11x2 double

I tried using data{:,9}(end) to get the last element from the column 'frac', but not working.

Upvotes: 1

Views: 51

Answers (1)

Suever
Suever

Reputation: 65430

The issue is that data{:,9} returns two elements.

data{:,9}

%   ans = 
%       'R_A'
%
%   ans = 
%       11 x 2 double

As a result, indexing with (end) after that isn't going to work. It looks like you only want the second row and not all of them. So something like this should work:

data{2,9}(end)

If you do want the last element from all things in column 9, then you'd need to use cellfun to do this for you.

values = cellfun(@(x)x(end), data(:,9), 'uniform', 0);

Upvotes: 2

Related Questions