Difficulty Assigning String Values after Regexp
Show older comments
Hi all! I had previously posted about trying to find keywords in a sentence, and I was sent to the regexp page (very helpful, thank you) which allowed me to do just that.
Now that it's possible for me to find the strings i'm looking for, I want matlab to assign each found string its corresponding value, and then multiply these values.
For example, the strings in the array "Good Object" all have a value of 1, and those in "Bad" all have a value of -1. so if i wrote "hurting your brother" regexp would find the keywords in their arrays, and then would return (-1)*(1) = -1.
so far with regexp it correctly finds the string's location in the array and prints it, but i'm having difficulties associating the words with their array values and multiplying them. below is the error i get, but i'm pretty sure it's just the tip of the iceberg. Any help would be greatly appreciated!
thank you
??? Too many outputs requested. Most likely cause is missing [] around
left hand side that has a comma separated list expansion.
Error in ==> goodbad at 17
x = (GoodObjectValue{Gov});
code:
GoodObject = {'brother', 'sister', 'mother', 'father'};
GoodObjectValue = {1};
Bad = {'hurting', 'stealing', 'robbing', 'breaking'};
BadValue = {-1};
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
question = input ('what?','s')
GoodExpression = GoodObject;
BadExpression = Bad;
matchstr = regexp(question,GoodExpression,'match')
matchstr = regexp(question, BadExpression,'match')
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
if matchstr(question,GoodObject)&& matchstr(question,Bad)
disp (i)
if (i == -1)
disp('that''s bad')
else if (i == 1)
disp('that''s good')
end
end
end
Accepted Answer
More Answers (1)
David Sanchez
on 20 Jun 2013
you did not define neither the value of Gov nor bv.
Your code:
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
It should be something like:
Gov = find(strcmpi(question,GoodObject));
Bv = find(strcmpi(question,Bad));
x = (GoodObjectValue{Gov});
y = (BadValue{Bv});
i=x*y;
3 Comments
David Sanchez
on 20 Jun 2013
could you show the new code and the error again?
kenny
on 20 Jun 2013
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!