regexp returns unwanted 0x0 string cells

2 views (last 30 days)
Hello,
I have a string array c that contains :
c = ["v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656" "n 0.064545 0.997915 -0.000000" "n 0.021679 0.999765 -0.000000"]
c = 1×4 string array
"v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656" "n 0.064545 0.997915 -0.000000" "n 0.021679 0.999765 -0.000000"
From that, I want to extract all the v so that my result looks like that :
["v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656"]
ans = 1×2 string array
"v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656"
To do so I use this regex expression :
'v (-?[0-9. ]+){3}'
ans = 'v (-?[0-9. ]+){3}'
I tried it on regex101 and it gives me the right results. However when I use it with Matlab, it gives me two unwanted empty string cells like this :
regexp(c,'v (-?[0-9. ]+){3}','match')
ans = 1×4 cell array
{["v -0.618008 0.394531 -0.972656"]} {["v -0.621094 0.391446 -0.972656"]} {0×0 string} {0×0 string}
This is a problem because I want my end result to be an array of string so I want to use string() on this result, but string returns an error with the {0x0 string}...
Do you know how I can skip the empty string cells with the regexp function ?

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jul 2022
Edited: Walter Roberson on 6 Jul 2022
regexp() accepts for its first parameter:
  • character vector
  • string scalar
  • cell array of character vectors
  • nonscalar string array
In the case of nonscalar inputs, it returns a cell array of the output of processing each input.
Your 3rd and 4th string entries do not match anything, so processing them returns empty, leading to the empty cell entries.
This is how regexp is designed to operate.
You have two possibilities:
  • you can use logical indexing by ~cellfun(@isempty) to filter out the entries you do not want; or
  • you can leave the original lines unsplit, a character vector or string scalar, instead of string array. fileread() instead of readlines()

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!