# Matrix dimensions must agree error in if loop

1 visualizzazione (ultimi 30 giorni)
Ellen De Jonghe il 15 Gen 2020
Commentato: Ellen De Jonghe il 15 Gen 2020
Why do I get a matrix dimensions error here?
I'm alson not sure about the num2str parts in the disp. Do I even have to convert day to a string because it is a string right?
day = input('What day is today?', 's');
if day == 'Saturday' | day == 'Sunday'
disp(['Its ' num2str(day) ' ! Its weekend!'])
else
disp(['Its ' num2str(day) ' ! Get to work!'])
end
>> whichDay
What day is today? sunday
Matrix dimensions must agree.
Error in whichDay (line 2)
if day == 'Saturday' | day == 'Sunday'
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Andrei Bobrov il 15 Gen 2020
Modificato: Andrei Bobrov il 15 Gen 2020
day = input('What day is today? -> ', 's');
lo = any(strcmpi(day,{'saturday','sunday'}));
if lo
disp(['Its ' day ' ! Its weekend!'])
else
disp(['Its ' day ' ! Get to work!'])
end
Illustration to the error you received:
day = input('What day is today? -> ', 's');
What day is today? -> Sunday
>> day == 'Saturday' | day == 'Sunday'
Matrix dimensions must agree.
>> day == 'Saturday'
Matrix dimensions must agree.
>> day == 'Sunday'
ans =
1×6 logical array
1 1 1 1 1 1
>>
##### 1 CommentoMostra NessunoNascondi Nessuno
Ellen De Jonghe il 15 Gen 2020
Thanks!
Do you also know why i get the error?

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by