Matlab regex if statement

Go To StackoverFlow.com

1

I want to have matlab take user input but accept both cases of a letter. For example I have:

function nothing = checkGC(gcfile)
if exist(gcfile)
    reply = input('file exists, would you like to overwrite? [Y/N]: ', 's');
    if (reply == [Yy])
        display('You have chosen to overwrite!')
    else
        $ Do nothing
    end
end

The if statement obviously doesn't work, but basically I want to accept a lowercase or uppcase Y. Whats the best way to do this?

2012-04-04 22:29
by E.Cross


5

Use the functions lower or upper. E.g.:

if (lower(reply) == 'y')

Alternatively, strcmpi will compare strings case-insensitively. E.g.:

if (strcmpi(reply, 'y'))
2012-04-04 22:36
by Franck Dernoncourt


2

Simply use a basic or operator in your condition. Check the documentation here: http://www.mathworks.se/help/techdoc/ref/logicaloperatorselementwise.html

2012-04-04 22:36
by Pico


1

http://www.mathworks.de/help/techdoc/ref/regexpi.html

if (regexpi(reply,'^y(es)?$'))
        display('You have chosen to overwrite!')
2012-04-04 23:23
by d_inevitable
Ads