I am getting the error "Variable y must be of data type char. It is currently of type double."

34 vues (au cours des 30 derniers jours)
I am getting the error "Variable y must be of data type char. It is currently of type double. Function should return with a character vector conataining only the email address." I'm not sure how to fix it please help.
function result = extractemail(input)
text = regexp(input,'[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+.[a-z]+');
email = regexp(input,'[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+.[a-z]+','match');
if(numel(text)==1)
result = char(email)
else
result = []
end
end
CODE TO CALL FUNCTION
result = extractemail('My email address is: raymond.kim@usc.edu so please email me');
OUTPUT
result =
'raymond.kim@usc.edu'

Réponses (1)

Stephen23
Stephen23 le 21 Avr 2022
Modifié(e) : Stephen23 le 21 Avr 2022
extractemail('My email address is: raymond.kim@usc.edu so please email me')
ans = 'raymond.kim@usc.edu'
extractemail('There is no email here')
ans = 0×0 empty char array
function txt = extractemail(input)
rgx = '[a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+.[a-z]+';
txt = regexp(input,rgx,'match','once');
end
Note that defining a regular expression to match valid email adresses is not trivial (or perhaps solvable):

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by