I need only words when i split a string , while currently i am getting even spaces

string2=split(string(result{1,hh}(ss,1)),[" ",",","(",")","-"])
>> string2
string2 =
8×1 string array
"Secure"
"VPDM"
"to"
"Ebay"
"housing"
""
"optional"
""
I need the output to be
"Secure"
"VPDM"
"to"
"Ebay"
"housing"
"optional"

 Accepted Answer

Probably the easiest is to do:
string2 = regexp(yourstring, '\w+', 'match')

10 Comments

I can sometimes have numbers in there too
What should i do then?
Caution: \w will not match ' or - characters or period.
Don't pay $29.95 for e-mail!
Would get broken into
Don
t
pay
29
95
for
e
mail
It can be difficult to define what "words" are in English.
string() the cell array that regexp returns.
\w already includes the digits. It would not include decimal points or commas or positive or negative signs or scientific notation. If you need to include numbers it is best to define exactly which numeric formats you will support.
Floating point numbers are always a nuisance to write expressions for, especially if you permit leading 0 to be omitted such as .716e-3 instead of 0.716e-3
>> (regexp(string2, '\w+', 'match'))
ans =
8×1 cell array
{["Secure" ]}
{["VPDM" ]}
{["to" ]}
{["Ebay" ]}
{["housing" ]}
{0×0 string }
{["optional"]}
{0×0 string }
Error using string
Conversion from cell failed. Element 6 must be
convertible to a string scalar.
This is the issue
Can you attach your string2 and/or result in a .mat file so we can use it?
save('answers.mat', 'string2', 'result');
Is string2 a string scalar or a non-scalar string array?
string2 comes out as missing in that file, and there is no way that that result variable is the output of that regexp() command.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!