I have an charecter array that contains so many characters and I want to call 4 th character from each row. How can I do it ?
For example consider that I have 6x1 character matrix like
Adana
mersin
hatay
iskenderun
cemre
cebir
I want to call 4 th character of each row and form a new 6x1 matrix with them like
n
s
a
e
r
i

댓글 수: 1

Guillaume
Guillaume 2018년 1월 22일
What you have is either
  • a 6x1 cell array, where each cell is a 1xN char array
  • a 6x1 string array (only in R2016b or later)
This is definitively not a 6x1 character array, which by definition, would have 6 letters vertically and only one letter horizontally.

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

 채택된 답변

Birdman
Birdman 2018년 1월 22일
편집: Birdman 2018년 1월 22일

0 개 추천

str='adana mersin hatay iskenderun cemre cebir';
str=regexp(str,'[a-z]*','match');
for i=1:numel(str)
a{i}=str{i}(4);
end
or instead of for loop:
cellfun(@(x) x(4),str)
EDIT:
After Guillaume's warning, different and easier approach for constructing the demo data and solution:
str='adana mersin hatay iskenderun cemre cebir';
str=strsplit(str,' ');
cellfun(@(x) x(4),str)

댓글 수: 3

I think that your method of constructing the cell array could be confusing, particularly the use of regexp to split the string into a cell array. The OP could think that it is part of the solution whereas it's only used to build the demo data.
str = {'adana'; 'mersin'; 'hatay'; 'iskenderun'; 'cemre'; 'cebir'};
%or
str = strsplit('adana mersin hatay iskenderun cemre cebir');
would be better in my opinion.
Birdman
Birdman 2018년 1월 22일
Yes, I understood what you mean about it. Thanks.
Gokhan Kayan
Gokhan Kayan 2018년 1월 22일
Thank you so much :)

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 22일

2 개 추천

"For example consider that I have 6x1 character matrix like"
No, a 6 x 1 character matrix cannot have multiple characters in the same row. You might have a 6 x 1 string matrix, or you might have a 6 x 1 cell array of characters.
If you have 6 x 10 (or wider) character array A, then A(:,4)
If you have a 6 x 1 cell array of characters or a 6 x 1 string array A then one of the approaches is cellfun(@(S) S(4), A) . In the case of string arrays, another approach is extractBetween(A, 4, 4) {with no loop} -- but note that this would return a 6 x 1 string array with each string being a single character long, so if you wanted the results as 6 x 1 char then you would need to char() the result of the extractBetween()

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

질문:

2018년 1월 22일

댓글:

2018년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by