フィルターのクリア

How to search for substring in cell array and get the corresponding index then?

1 回表示 (過去 30 日間)
Paul Hinze
Paul Hinze 2022 年 12 月 7 日
回答済み: Askic V 2022 年 12 月 7 日
Dear community,
i have a very big 3D cell array that contains either doubles or strings. I need to find the cells that contain the string EXPERIMENT with the given index of my cell array.
I already tried approaches like strcmp or strfind, but I am still stuck.
Below is a picture of a subset of the cell array.
Thank you in advance,
Paul

採用された回答

Askic V
Askic V 2022 年 12 月 7 日
Please have a look at the following example:
C1 = {'Smith','Chung','Morales' [4 5 6]; ...
'Sanchez','Peterson',1:15,'Adams';...
'Adams','Johnson',[2.1 4.2],'Adams'}
C1 = 3×4 cell array
{'Smith' } {'Chung' } {'Morales' } {[4 5 6]} {'Sanchez'} {'Peterson'} {[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]} {'Adams'} {'Adams' } {'Johnson' } {[ 2.1000 4.2000]} {'Adams'}
C2 = cellfun(@num2str,C1,'UniformOutput',false)
C2 = 3×4 cell array
{'Smith' } {'Chung' } {'Morales' } {'4 5 6'} {'Sanchez'} {'Peterson'} {'1 2 3 4 5 6 7 8 9 10 11 12 13 14 15'} {'Adams' } {'Adams' } {'Johnson' } {'2.1 4.2' } {'Adams' }
C3 = strfind(C2, 'Ada')
C3 = 3×4 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[ 1]} {[ 1]} {0×0 double} {0×0 double} {[ 1]}
ind = ~cellfun('isempty', C3)
ind = 3×4 logical array
0 0 0 0 0 0 0 1 1 0 0 1
I think you should be able to use it in your own application. C1 becomes your val

その他の回答 (1 件)

GeeTwo
GeeTwo 2022 年 12 月 7 日
If I'm understanding correctly,
cellfun(@numel,strfind(string(val),"EXPERIMENT"))
will give you a matrix with 1's where the string is found and 0's where it isn't, whether due to it being a string without "EXPERIMENT" or not a string at all.
  1 件のコメント
Paul Hinze
Paul Hinze 2022 年 12 月 7 日
Hi
if i enter; cellfun(@numel,strfind(string(stimuli),"EXPERIMENT")), where stimuli is my variable i get following error message: Error using string
Conversion from cell failed. Element 1 must be convertible to a string scalar.

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

カテゴリ

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