フィルターのクリア

Search for individual characters within string

5 ビュー (過去 30 日間)
Ole Hansen
Ole Hansen 2012 年 6 月 17 日
Say I have:
cell_array={'AX','BY'};
It is easy to manipulate the terms 'AX' and 'BY' (to isolate, sort, identify etc.), but I cannot seem to find an approach to search for the individual components within 'AX' or 'BY'.
I would like to consider each character in the string individually, i.e. 'A' or 'X' in 'AX'. Basically, I want to be able to search for 'A' or 'X' in 'AX' without having to modify 'AX'.
Is that possible?

採用された回答

per isakson
per isakson 2012 年 6 月 17 日
Run this
cell_array = {'AX'; 'BY'; 'CXXYXXYYYX'; 'DYXZZZXXYXZ'};
cac = cellfun( @(c) strfind( cell_array, c ), {'A','Y'}, 'uni', false );
cac{2}
cac{2}{3}
ans =
[]
[ 2]
[1x4 double]
[1x2 double]
ans =
4 7 8 9
>>
Which says that the letter, "Y", exists in the second, third and forth string. In the third string "Y" appears in position 4,7,8 and 9.

その他の回答 (2 件)

Image Analyst
Image Analyst 2012 年 6 月 17 日
Try to adapt this demo code:
% Construct a sample cell array.
cell_array={'AX'; 'BY'; 'CXXYXXYYYX'; 'DYXZZZXXYXZ'};
% Now find X and Y locations for each row.
for k = 1 : size(cell_array, 1)
% For each row in the cell array...
% Get the string inside the cell.
cellContents = cell_array{k}
% Find arrays giving the indexes of X and Y.
xIndexes = strfind(cellContents, 'X')
yIndexes = strfind(cellContents, 'Y')
end

Rui Zhao
Rui Zhao 2012 年 6 月 17 日
For searching for 'x' in 'AX', you can search the uppercase of input via doing this:
char(double('x') - 32); % translate the 'x' into ASCII number and subtract 32 to get uppercase 'X'.
For searching lowercase, vice versa. char(double('X') + 32);
  2 件のコメント
Ole Hansen
Ole Hansen 2012 年 6 月 17 日
Thanks for your comment. It is not, however, an issue about lower and upper case. It is about searching for individual characters within a string, and about the position of the first and second character (the individual terms are always of length 2).
Image Analyst
Image Analyst 2012 年 6 月 17 日
Isn't the position of the first character always 1 and the position of the second character always 2?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by