Bird
0
0

You have a cell array of strings: {'abc123', 'def456', 'ghi789'}. How do you extract only the numeric parts from each string using MATLAB regular expressions?

hard📝 Application Q15 of 15
MATLAB - String Handling
You have a cell array of strings: {'abc123', 'def456', 'ghi789'}. How do you extract only the numeric parts from each string using MATLAB regular expressions?
AUse <code>regexp(cellArray, '\\d+', 'match')</code>
BUse <code>regexp(cellArray, '\\w+', 'match')</code>
CUse <code>regexprep(cellArray, '\\D', '')</code>
DUse <code>regexprep(cellArray, '\\d+', '')</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to keep only digits, removing all non-digit characters.
  2. Step 2: Choose the correct function and pattern

    regexprep replaces patterns. Using \\D matches non-digits, so replacing them with empty string keeps only digits.
  3. Final Answer:

    Use regexprep(cellArray, '\\D', '') -> Option C
  4. Quick Check:

    Remove non-digits with \D pattern [OK]
Quick Trick: Replace non-digits (\D) with empty string to keep digits [OK]
Common Mistakes:
  • Using regexp with 'match' returns cell arrays, not strings
  • Replacing digits instead of non-digits
  • Using \w+ which matches letters and digits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes