MATLAB Answers

how to index a cell array?

60 views (last 30 days)
YH
YH on 13 Nov 2019
Commented: YH on 18 Nov 2019
Hallo,
I have a structure (K) with many fields, containg a field (elevation) which is (161*1 cell) array, each cell is a matrix (n*1) double.
I wanted to find all the elements in each matrix that is more than 0 then calculate the area of these values (above 0)
firstly I made a new cell array of the indexs that I needed
idx = arrayfun(@(K) K.elevation >=0, K, 'UniformOutput', false)';
now I want to use the function trapz to find the area
I tried many things, but I'm getting always errors like 'Unable to use a value of type cell as an index or 'Expected one output from a curly brace or dot indexing expression, but there were 161 results'
Can someone help me with this issue?
many thanks!

Accepted Answer

Shubham Gupta
Shubham Gupta on 14 Nov 2019
Try :
area_r = arrayfun(@(a) cellfun(@(c) trapz(c(c>=0)),a.elevation,'UniformOutput',false), K, 'UniformOutput',false);
I hope it helps !
  3 Comments
YH
YH on 18 Nov 2019
thank you @Shubham Gupta, that was really helpful.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!