how to find out the index of a cell array that cell contains my substring?

My question is:
myCellArray = {'John','Mike','foo'};
substring = 'Jo'
Since myCellArray{1} contains "Jo', I expected the index = 1.
How to use cellfun to find the index of the element(s) in the cell array?

2 件のコメント

Rui Zhang
Rui Zhang 2024 年 2 月 15 日
My Matlab version is 2017b
Rui Zhang
Rui Zhang 2024 年 2 月 15 日
If I don't use cell function, the following code should work:
myCellArray = {'John','Mike','foo'};
substring = 'Jo';
indexForMySubstring = zeros([1,numel(myCellArray)]);
kk = 1;
for ii =1: numel(myCellArray)
if contains(myCellArray{ii},substring)
indexForMySubstring(kk) = ii;
kk = kk+1;
end
end
if kk>1.5
indexForMySubstring = indexForMySubstring(1:kk-1);
else
indexForMySubstring = [];
end
This will return array of the indexes that contains 'Jo' in myCellArray
But this code runs too slow when it is used for a large application.
I am looking for a solution that runs faster than this like using cellfun.

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

 採用された回答

Fangjun Jiang
Fangjun Jiang 2024 年 2 月 15 日
Do you have to use cellfun()?
myCellArray = {'John','Mike','foo'};
substring = 'Jo'
substring = 'Jo'
find(contains(myCellArray,substring))
ans = 1

7 件のコメント

Rui Zhang
Rui Zhang 2024 年 2 月 15 日
This code doesn't work for MATLAB 2017b. When I ran your code in 2017b, I got the following error message:
Error using contains (line 40)
First argument must be a string array, character vector, or cell array of
character vectors.
Fangjun Jiang
Fangjun Jiang 2024 年 2 月 15 日
Try this?
cellfun(@(a) contains(a,substring),myCellArray)
Adam Danz
Adam Danz 2024 年 2 月 15 日
編集済み: Adam Danz 2024 年 2 月 15 日
I just ran @Fangjun Jiang's solution in 17a and 17b and there were no errors.
@Rui Zhang, are you testing an exact copy of Fangjun's code that uses a cell array of character vectors? There shouldn't be a need for cellfun.
Fangjun Jiang
Fangjun Jiang 2024 年 2 月 15 日
Right. "First argument must be a string array, character vector, or cell array of character vectors."
contains(myCellArray,substring)
myCellArray is a cell array of character vectors.
Rui Zhang
Rui Zhang 2024 年 2 月 15 日
編集済み: Rui Zhang 2024 年 2 月 15 日
Not the same code.
In my code, the cell array includes empty elements. It looks like the following:
myCellArray ={[],'John','','Mike','foo'};
substring = 'Jo';
myIndex = find(contains(myCellArray,substring));
Error using contains
First argument must be text.
My code is designed to read data from an excel spreadsheet, including text. The excel spreadsheet includes header (column title) and data. By giving a keyword that is part of the element in the header cell array, I should find out the column and take the data from that column.
For example,
myCellArray = {[] 'John','Mike','foo';...
0 10 11 20;...
1 12 13 22;...
2 14 15 18;...
3 15 20 25};
% give a key word 'Joh' I should take the second column of the data; that
% should be:
myColumnIndex = 2;
johnData = cell2mat(myCellArray(2:end,myColumnIndex));
My question is: how to find out myColumnIndex (2) by searching the first row of the text?
myCellArray = {[],'John','','Mike','foo'};
substring = 'Jo';
% replace non-char entries with empty chars:
fixedCellArray = myCellArray;
fixedCellArray(~cellfun(@ischar,fixedCellArray)) = {''};
myIndex = find(contains(fixedCellArray,substring))
myIndex = 2
Rui Zhang
Rui Zhang 2024 年 2 月 16 日
Good solution. Thank you!

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

その他の回答 (1 件)

Aquatris
Aquatris 2024 年 2 月 15 日
編集済み: Aquatris 2024 年 2 月 15 日
Using the @Fangjun Jiang example, how about
myCellArray = {'John','Mike','foo','Jonathan','Stuart','Martha','Jo'};
substring = 'Jo';
idx = find(cellfun(@(x) contains(x,substring),myCellArray,'UniformOutput',true))
idx = 1×3
1 4 7
myCellArray(idx)
ans = 1×3 cell array
{'John'} {'Jonathan'} {'Jo'}

4 件のコメント

Rui Zhang
Rui Zhang 2024 年 2 月 15 日
I tested your code and it works for me.
But if myCellArray include an empty element, it doesn't work. The key to make it work is how to deal with empty elements in a cell array.
I have no idea how to do it.
Thank Aquatris and Fangjun for helping me out. I really appreciate it.
Aquatris
Aquatris 2024 年 2 月 15 日
編集済み: Aquatris 2024 年 2 月 15 日
Can you explain what you mean by it does not work when you have an empty element?
myCellArray = {'John','Mike','foo','','Jonathan','Stuart','','Martha','Jo'}
myCellArray = 1x9 cell array
{'John'} {'Mike'} {'foo'} {0x0 char} {'Jonathan'} {'Stuart'} {0x0 char} {'Martha'} {'Jo'}
substring = 'Jo';
idx = find(cellfun(@(x) contains(x,substring),myCellArray,'UniformOutput',true));
myCellArray(idx)
ans = 1x3 cell array
{'John'} {'Jonathan'} {'Jo'}
Aquatris
Aquatris 2024 年 2 月 15 日
編集済み: Aquatris 2024 年 2 月 16 日
Not the cleaness solution but I think you are looking for something like this then:
myCellArray = {[] 'John','Mike',[],'foo','Johanna','Mark',[];...
0 10 11 20 30 40 50 60;...
1 12 13 22 60 70 80 20;...
2 14 15 18 40 20 10 20;...
3 15 20 25 30 40 50 60};
substring = 'Jo';
idx_empty = cellfun(@isempty,myCellArray(1,:)); % find empty cells in first row
myCellArray_Modified = myCellArray(1,:); % create replica of first row of myCellArray
myCellArray_Modified(idx_empty) = {-1}; % replace empty cells with -1 double
idx = find(cellfun(@(x) contains(string(x),substring),myCellArray_Modified(1,:),'UniformOutput',true));
idx % idx is the column numbers of interest
idx = 1×2
2 6
myCellArray(:,idx)
ans = 5×2 cell array
{'John'} {'Johanna'} {[ 10]} {[ 40]} {[ 12]} {[ 70]} {[ 14]} {[ 20]} {[ 15]} {[ 40]}
Rui Zhang
Rui Zhang 2024 年 2 月 16 日
Thank you! It really helps me!

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

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

製品

リリース

R2017b

質問済み:

2024 年 2 月 15 日

コメント済み:

2024 年 2 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by