MATLAB Answers

How to find columns by name in a cell aray

16 views (last 30 days)
012786534 on 11 Apr 2017
Commented: 012786534 on 11 Apr 2017
Hello all,
Let's say I have a large cell array with similar variable names in the first row (A_F1 , B_F2 , A_F2 , G_F5 ...) followed by data.
I want to create a smaller cell array with just the columns where the variable name starts with A.
How would I do that?
Thank you


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 11 Apr 2017
Edited: Stephen Cobeldick on 11 Apr 2017
This is easy with strncmp:
>> inp = {'A_F1','B_F2','A_F2','G_F5';1,2,3,4}; % demo matrix.
>> idx = strncmp(inp(1,:),'A',1); % indices of strings starting with A.
>> otp = inp(:,idx); % select those columns from inp.
As an alternative you could use the table class, and this task may well be easier.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!