Match Substring in Cell Array and Copy To New Array

2 views (last 30 days)
Hello,
I'm attempting to match a substring in a 9k*1 cell array and copy it to a new array. Ideally I also would like to find where the substring start in the string (i.e. the char number), but I think it's better to do it one bridge at a time....
Sample strings:
  • school of medecine university of CA in Los Angeles
  • engineering programs in california
  • University of CA in Los Angeles degrees
  • University of CA in Irvine nursing online studies
  • rankings of universities in california
  • school of mathematics UCLA
  • financial aid UCLA
  • UCLA minorities financial aid
Desired output for example: ucla_programs= [ school of medecine university of CA in Los Angeles school of mathematics UCLA ]
ucla_financial= [ financial aid UCLA UCLA minorities financial aid ]
So far I've tried various versions of regexpi, such as;
for j =1:length(keywords)
%keywords(j)
if ~isempty(regexpi(keywords(j), 'UCLA', 'start'))
keywords(j);
end
end
But, this doesn't work, let alone it (suppose to ) return empty sets rather than the relevant sets.
I know how to do string manipulation in python/c++, but matlab isn't the most intuitive for me in this instance. I'd appreciate any suggestions/references you have.
Thank you,

Accepted Answer

per isakson
per isakson on 30 Jun 2013
Edited: per isakson on 30 Jun 2013
I understand approximately half of your question. Not more because
  • "substring start" doesn't match "ucla_financial= [ financial aid UCLA UCLA minorities financial aid ]". The example suggests that you search strings, which contain the two words: financial and UCLA, not a substring
  • The example "ucla_programs= [ school of medecine university of CA in Los Angeles school of mathematics UCLA ]" is not helpful. The word, programs, doesn't appear in the selected strings.
Anyhow, here is an attempt
cac = { 'school of medecine university of CA in Los Angeles'
'engineering programs in california'
'University of CA in Los Angeles degrees'
'University of CA in Irvine nursing online studies'
'rankings of universities in california'
'school of mathematics UCLA'
'financial aid UCLA'
'UCLA minorities financial aid' };
keywords = { { 'UCLA', 'financial' } };
for ii = 1 : size(keywords,1)
for jj = 1 : size(cac,1)
words = regexp( cac{jj}, '[ \.\,\?\!]+', 'split' );
if all( ismember( keywords{ii}, words ) )
disp( cac{jj} )
end
end
end
outputs
financial aid UCLA
UCLA minorities financial aid
  11 Comments
per isakson
per isakson on 1 Jul 2013
Edited: per isakson on 1 Jul 2013
This kind of "pair-programming" is prone to mistakes. It is only too easy to miss changes the other part makes. And the intention behind the changes and additions are not properly communicated.
.
Comments on the code in the comment above
The naming of the argument in the call of and the definition of the function, search_sub, could be more clear:
search_sub( keywords , search_term );
function search_substring=search_sub( current_array, current_keyword)
"now it searches through both elements". I doubt that. Your code does not work as intended (by me). Anyhow, in search_term = {'university' 'college'}; do you intend an AND or an OR? I assumed AND.
"transposes the search_term element,". I cannot see how that transpose affects the behavior of the code
Avoid globals! Replace search_substring by matched_keyword as output argument
.
Be aware that
  • all( ismember( cas1, cas2 ) ) in the function, search_sub, assumes that both arguments are cell array of strings. current_keyword{ii} is a string in the function, sub_search
  • ismember is case sensitive
.
Finally
Start using the debugger
per isakson
per isakson on 1 Jul 2013
Edited: per isakson on 1 Jul 2013
You write: "Part of the issue I think is that
size(current_keyword,1)
ans =
1
where is suppose to return 2, is that correct?"
.
I'm lost. The problem is that I don't fully understand your requirements and that you don't fully understand the code, which I propose.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!