Let's say I have an array C
C = {'frog'; 'cat'}
and I want to just pull the first letter of the first cell, is that possible?
For context, I am trying to use this in the regexpi function to search for a word that starts with the same letter as one of the terms in the array, but in a loop or else I could just use:
C = {'frog'; 'cat'}
exp = 'f\w*'
frog = regexpi(C, exp)
Not sure if it is possible, or maybe I'm just way over thinking it and the answer is super simple. Thanks!

 채택된 답변

Star Strider
Star Strider 2019년 4월 4일

0 개 추천

I can’t follow your regexpi application, however getting the first letters of the words in your cell array is almost trivial:
C = {'frog'; 'cat'};
ltr1 = cellfun(@(x)x(1), C)
producing:
ltr1 =
2×1 char array
'f'
'c'
Please don’t name your variable ‘exp’, since you may need to use the exponential function that shares that name. (This is called ‘overshadowing’ a builtin MATLAB function, and is discouraged.)

댓글 수: 2

Carlie Jones-Hershinow
Carlie Jones-Hershinow 2019년 4월 5일
Thank you! (And yes I realize that variable name isn't a good idea, I was just throwing together an example, it wasn't from my actual code, but thank you for looking out)
Star Strider
Star Strider 2019년 4월 5일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File 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