How can I search through a row in a cell array to find strings?

I have imported data as a cell array from excel via the xlsread function.
I have searched through the first column in the array using
for i= find(strcmp('string',filename)). This gives me the correct rows that I need to look through to find strings.
How do I search through these found rows to find strings? The array is full of empty spaces and strings. I do not need a specific string, I need any string within that row.
Thank you

2 件のコメント

sixwwwwww
sixwwwwww 2013 年 12 月 4 日
can you show your complete code and your data to ans the question in a better way
Justin
Justin 2013 年 12 月 5 日
編集済み: Justin 2013 年 12 月 5 日
Here is my code thus far:
%%Import data from spreadsheet
% Workbook: MyDATA.xlsx
% Worksheet: Data Correlation
%%Import the data
[~, ~, MyDATA] = xlsread('MyDATA.xlsx','Data Correlation');
MyDATA(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),MyDATA)) = {''};
for i= find(strcmp('DIRs',MyDATA));
%Rows=~all( cellfun( @isempty, MyDATA(i,:) ) );
Rows= find( ~cellfun( @isempty, MyDATA(i, :) ) );
end
Ultimately, I am trying to go through the rows for each DIR and see what Alerts happen within 36 hours of the DIR.

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

回答 (2 件)

Walter Roberson
Walter Roberson 2013 年 12 月 4 日

0 投票

find( ~cellfun( @isempty, YourArray(i, :) ) )
or if you just care whether there is anything non-empty
~all( cellfun( @isempty, YourArray(i,:) ) )

1 件のコメント

Justin
Justin 2013 年 12 月 9 日
編集済み: Justin 2013 年 12 月 10 日
~all( cellfun( @isempty, YourArray(i,:) ) )
seemed to work. It returned a 1 or a 0 for each column that contained a string in the rows labeled DIR.
find( ~cellfun( @isempty, YourArray(i, :) ) )
worked as well. This I believe returned cell numbers for each string within the rows labeled DIRs.
Thank you

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

sixwwwwww
sixwwwwww 2013 年 12 月 5 日
編集済み: sixwwwwww 2013 年 12 月 9 日

0 投票

Dear Justin, you can find the occurances of string 'DIRs' in cell array as follow:
[~, ~, MyDATA] = xlsread('filename.xlsx','Data Correlation');
MyDATA(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),MyDATA)) = {''};
str = 'DIRs';
StringFoundAt(1, :) = {'Row number', 'Starting index'};
count = 2;
for i = 1:size(MyDATA, 1)
row = MyDATA(i, :);
row = row(cellfun(@(x) (~isempty(x)) && (~isnumeric(x)), row));
if ~isempty(row)
idx = strfind(row, str);
idx = idx(cellfun(@(x) ~isempty(x), idx));
if ~isempty(idx)
StringFoundAt(count, :) = {i, cell2mat(idx)};
count = count + 1;
end
end
end
I hope it helps. Good luck!

5 件のコメント

Justin
Justin 2013 年 12 月 9 日
編集済み: Justin 2013 年 12 月 9 日
This code stores the first string in row 161, column 1 and any other string in row 161 for the row variable. It is only searching through the last row in MyDATA.
The 'Starting index' within the StringFoundAt variable shows 1 for every 'Row number'.
idx stores a blank cell.
The other variables I believe are functioning correctly. I wish I could explain better. Sorry, I'm not very good at this. Ha. Thank you.
sixwwwwww
sixwwwwww 2013 年 12 月 9 日
It is the result which I obtained from this code and data you provided in the attachment:
'Row number' 'Starting index'
4 1
10 1
16 1
22 1
28 1
34 1
40 1
46 1
52 1
58 1
64 1
70 1
76 1
82 1
88 1
94 1
100 1
106 1
112 1
118 1
124 1
130 1
136 1
142 1
148 1
154 1
160 1
Is this result you want? Is it correct?
Justin
Justin 2013 年 12 月 10 日
That is the result that I got as well. What exactly is the 'starting index'. Thank you
sixwwwwww
sixwwwwww 2013 年 12 月 13 日
Starting index means DIRs appears in the row given by Row number and at position given by Starting index. So
'Row number' 'Starting index'
4 1
means that string DIRs appears at start(because Starting index = 1) of the in row 4. Is that what you need?
Justin
Justin 2014 年 1 月 6 日
編集済み: Justin 2014 年 1 月 6 日
I know which rows contain DIRS in the first column. I was more wanting to find which columns within those DIR rows contain strings, not including the column that is labeled DIRS. Thank you.

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

カテゴリ

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

質問済み:

2013 年 12 月 4 日

編集済み:

2014 年 1 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by