MareB
MareB

Reputation: 77

Matlab: delete empty spaces in a string

I have a 400 x 8 char in which I would like to delete empty spaces. I have numbers like this:

1, 99278; 4, 99378; 1,101283;

I would need to have:

1,99278; 4,99378; 1,101283;

Thanks a lot!

Upvotes: 1

Views: 6422

Answers (4)

muhammed
muhammed

Reputation: 39

Another form to invoke strrep function:

strrep(string,' ','');

Upvotes: 2

Nischaal Cooray
Nischaal Cooray

Reputation: 210

Try this:

stringWithoutSpaces = originalString(~isspace(originalString))

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112759

It appears you want to remove spaces after commas, but not after semicolons. You can do that easily with regexprep:

>> str = '1, 99278; 4,  99378; 1,101283;';
>> result = regexprep(str, ',\s+', ',');
result =
1,99278; 4,99378; 1,101283;

Upvotes: 4

Hoki
Hoki

Reputation: 11812

The function isspace is your friend.

myString( isspace(myString) ) = [] ;
strrep( myString, ';' , '; ') ;

The first line is enough if you don't need a space after the ;. If you want to keep this space, run the second line too.

Upvotes: 2

Related Questions