MATLAB Answers

## How to choose subset of cell arrays?

Asked by Tintin Milou

### Tintin Milou (view profile)

on 21 Feb 2015
Latest activity Commented on by per isakson

### per isakson (view profile)

on 21 Feb 2015
Accepted Answer by Guillaume

### Guillaume (view profile)

Hi all,
I have the following problem: I have 30 experiments and would like to plot them several graphs. The user should be able to decide which experiments to plot on the same graph and which on different ones. For that I introduced a cell array, e.g.
plotselect = {[2 5],[4]}
Here, experiments 2 and 5 shall be plotted on the same graph, whereas experiment 4 shall be plotted on a second graph. I also allow the user to choose which experiments (out of 30) to run in the first place.
expselect = [1:4];
That runs the first four experiments. As you can see, the user might want to run more experiments than what he's actually going to plot. But the values in plotselect must be a subset of expselect. How can I retrieve the values in plotselect that are also in expselect? That is, I'd like to get
plotselect = {[2],[4]}

#### 0 Comments

Sign in to comment.

## 2 Answers

### Guillaume (view profile)

Answer by Guillaume

### Guillaume (view profile)

on 21 Feb 2015
Accepted Answer

As per's answer, you need to use intersect. A cellfun lets you keep the original structure of your plotselect:
plotselect = {[2 3 5], [3 4 6], 2};
expselect = 1:4;
plotselect = cellfun(@(v) intersect(v, expselect), plotselect, 'UniformOutput', false)

#### 0 Comments

Sign in to comment.

### per isakson (view profile)

Answer by per isakson

### per isakson (view profile)

on 21 Feb 2015
Edited by per isakson

### per isakson (view profile)

on 21 Feb 2015

Hint:
>> intersect( expselect, cell2mat( plotselect ) )
ans =
2 4
>>
This assumes that all cells in plotselect contain numerical rows or scalars.
After a second reading of the question
>> num2cell( intersect( expselect, cell2mat( plotselect ) ) )
ans =
[2] [4]
>>

Guillaume

### Guillaume (view profile)

on 21 Feb 2015
I believe that either solution lose the grouping of plots.
per isakson

### per isakson (view profile)

on 21 Feb 2015
Yes!

Sign in to comment.