# Operands to the || and && operators must be convertible to logical scalar values.

2,362 views (last 30 days)

Show older comments

Diana Das Angeloose
on 28 Jul 2014

Commented: Walter Roberson
on 25 Feb 2022

%%Spectrometer or Ionisation

if ~iscell(folders) && isnan(folders) && strcmpi(Type,'Io')

folders={'i1'};

elseif ~iscell(folders) && isnan(folders) && strcmpi(Type,'Sp1')

folders={'s1'};

is the code how to solve the error

Operands to the and && operators must be convertible to logical scalar values.Error in SearchMCfiles_mp (line 33) if ~iscell(folders) && isnan(folders) && strcmpi(Type,'Io')

##### 3 Comments

### Accepted Answer

Patrik Ek
on 28 Jul 2014

##### 3 Comments

Walter Roberson
on 30 Nov 2019

&& is the "short circuit" and operation, that does not evaluate the right hand side if the left hand side is false. But when you are working with vectors, true or false of the left hand side is element-by-element, and it isn't really possible to only evaluate the right hand side with respect to selected elements. So && and || are simply not permitted except on scalars, by definition.

Note: if you are using && or || chances are strong that you should be considering using any() or all() . Sometimes it make sense to use all(condition1)&&all(condition2)

### More Answers (4)

Evan Ekblaw
on 11 Sep 2020

Edited: Walter Roberson
on 24 Feb 2022

while xf>=360 && xf<=390

tries=tries+1;

[v0,theta]=swing_choices();

angle=theta*34*(pi/180);

v0=v0*(cos(angle));

xf=x0+v0*t+.5*a_x*t.^2;

plot(xf,y,"--k")

end

Dont see the error here.

##### 1 Comment

Steven Lord
on 11 Sep 2020

What is the size of xf when that first line executes and throws that error?

[true false] && [true true] % Will throw this same error

[true false] & [true true] % Will return a 1-by-2 logical array

You likely want to use & and wrap that condition in any or all.

Megha S
on 2 Jun 2019

Operands to the || and && operators must be convertible to logical scalar values.

Error in vol_down (line 8)

if((dwn==0)||(dwn==1))

Patrick Benz
on 17 Mar 2021

Edited: Patrick Benz
on 17 Mar 2021

I get the same error.

Tiefe<(mu_Tiefe-4*sigma_Tiefe)|| Tiefe>(mu_Tiefe+4*sigma_Tiefe)

Tiefe is an 696x1 array.

When I'm only copying the left or the right part into the command Window, I get logical arrays with 696x1.

When I change to code to:

Tiefe<(mu_Tiefe-4*sigma_Tiefe)| Tiefe>(mu_Tiefe+4*sigma_Tiefe)

it works. But do I have a logical error in my code?

I want to check if every value in the array "Tiefe" is matching one of the two conditions.

Or is there a better option to compare arrays with scalar values?

##### 6 Comments

lakshmi Shree B
on 24 Feb 2022

Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.

Error in test_ppg (line 9)

if (file == 0) && (path == 0)

##### 5 Comments

Walter Roberson
on 25 Feb 2022

[file, filedir] = uigetfile('*.dat');

if isnumeric(file)

return; %user cancel

end

filename = fullfile(filedir, file);

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!