0

I have a cell array, strings of months in MATLAB. I am using the contains() function to determine if 'Ju' is in the string, along with an if statement, and if the statement is true, then delete 'Ju.' But I am getting an error. The error is: Undefined function or variable 'a'.

Error in Untitled (line 4) if a == contains(months, pattern)

pattern = 'Ju';
months = {'June', 'July', 'August', 'September'};

if a == contains(months, pattern)
    a = regexprep(months, 'Ju', '')
end
1
  • What error are you getting? Commented May 1, 2019 at 15:26

2 Answers 2

2

For your example, you don't even need an if statement. regexprep can handle it all for you:

>> pattern = 'Ju';
>> months = {'June', 'July', 'August', 'September'};
>> a = regexprep(months, pattern, '')

a =

  1×4 cell array

    'ne'    'ly'    'August'    'September'
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set a before testing it:

>> a = contains(months, pattern)

a =

  1×4 logical array

  1   1   0   0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.