match the first letter in a cell and save

조회 수: 1 (최근 30일)
Vincent I
Vincent I 2013년 6월 12일
Hi, without using a loop for the cell I would like to the the following: I have a 5x1 cell and I would like to check to see if the first letter starts with an 'P'. if it does then I would like to save the cell to another variable eg:
variable<5x1 cell>=
A string 1 - cell 1
P string 2 - cell 2
P string 3 - cell 3
A string 4 - cell 4
P string 5 - cell 5
new_variable<3x1 cell>=
P string 2 - cell 2
P string 3 - cell 3
P string 5 - cell 5

채택된 답변

Kye Taylor
Kye Taylor 2013년 6월 12일
편집: Kye Taylor 2013년 6월 12일
Try this (I assume your cell is called variable)
idx = cellfun(@(s)strncmp(s,'P',1),variable)
newCell = variable(idx);
note that strncmp function is case sensitive so be sure you have 'P' and not 'p'. If you want to detect both lower and upper case p, use this command
idx = cellfun(@(s)strncmp(s,'P',1)|strncmp(s,'p',1),variable)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by