Reputation: 249
I have a cell array (n-by-1 dimension) containing both strings and cells, which looks something like this
{
{'r1'} % -> cell content is in format 'char'
{'r2'} % -> cell content is in format 'char'
{1x2 cell} % -> cell content is a cell array: [{'r1'}{'r2'}]
{'r3'} % -> cell content is in format 'char'
{1x2 cell} % -> cell content is a cell array: [{'r1'}{'r3'}]
{1x2 cell} % -> cell content is a cell array: [{'r2'}{'r3'}]
{1x3 cell} % -> cell content is a cell array: [{'r1'}{'r2'}{'r3'}]
...
}
I need to find the row-index where the some string is included, e.g. 'r2'. I usually use strfind
for this purpose which works great if the cell array has a consistent format (hence 'char'-format within each cell).
Is there any way to apply this function to the cell array structure which is displayed above?
Thanks!
EDIT: Please find attached three images showing the data structure that I am using, since I am not sure how to exactly show/explain the hierarchies and layers of the cell array in text. Hope that helps. Also find attached the outcome of the code.
Code used:
change = 'r1.m';
srch = cellfun(@(x) strfind(x, change), strats, 'UniformOutput', false);
stringInRow = cellfun(@(x) numel(x) == 1 || (numel(x)>1)*numel(cell2mat(x))>0, srch);
rows = find(stringInRow);
Upvotes: 0
Views: 650
Reputation: 73176
You could use two subsequent cellfun
calls: one to perform the string search (cell by cell), and one to evaluate it (found string true
or not false
?)
%// example data
c{1} = 'r1';
c{2} = 'r2';
c{3}{1} = 'r1'; c{3}{2} = 'r2';
c{4} = 'r3';
c{5}{1} = 'r1'; c{5}{2} = 'r3';
%// example search
searchForString = 'r2';
srch = cellfun(@(x) strfind(x, searchForString), c, 'UniformOutput', false);
stringInRow = ...
cellfun(@(x) numel(x) == 1 || (numel(x)>1)*numel(cell2mat(x))>0, srch);
%// ^-. short-circuit scalars here ^
%// |
%// since cell2mat is valid only for cells or empty arrays
With resulting stringInRow
:
stringInRow =
0 1 1 0 0
If you want to explicitly list the rows, you can just make use of find
on the stringInRow
boolean vector
>> foundStringInRows = find(stringInRow)
foundStringInRows =
2 3
Upvotes: 3