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

1 view (last 30 days)
C.G. on 20 Jan 2021
Commented: C.G. on 21 Jan 2021
I am trying to run a series of data thorugh a if loop, to sum the amount of numbers between -0.13 and -0.16 found in each.
When i run this code, i get the error: Operands to the || and && operators must be convertible to logical scalar values.
Can anybody help?
files = dir('*.csv'); %dir lists the files in a folder. In the specified folder, recognise all the .csv files
num_files = length(files); %specify how many files have been found and call it num_files
particledata = cell(length(files), 1); %create a cell array the same length as the number of files in one column
%for all the files found in the specified folder, read the tables of data and fill the empty cell array 'results' with the data in each .csv file
for a = 1:num_files
particledata{a} = readtable(files(a).name);
ymax = -0.13;
ymin = -0.16;
for b = 1:length(particledata)
%save all the rows in the 6th column (x-coordinates) of each cell as a new variable x
x{b} = particledata{b}(:,5);
%use the function table2array to turn the format of the data from a table to an array
x_array{b} = table2array(x{b});
%for all the rows in the array, if the x coordinate goes beyond the outlet
%of the rice pile, display 'grain left rice pile'
if x_array{b}>ymin & x_array{b}<ymax
disp('grain left rice pile');
%sum the total number of grains leaving the rice pile in each cell, and save into a new variable 'grains'
grains{b}=sum(x_array{b}>ymin) && (x_array{b}<ymax)
fprintf('A total of %d grains left the rice pile\n',grains{b});
totalruntime = num_files;
time = 1:totalruntime;
%convert the cell file to a double file to allow plotting of data
grains = cell2mat(grains);
%mass efflux vs. time
%plot 'grains left rice pile' vs. time step as a bar graph
%0.01 sets the width of each bar
bar(time,grains, 0.01, 'EdgeColor', 'r')
title('mass efflux')
xlabel('Time (S)')
ylabel('Mass Efflux (No. of Particles)')

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jan 2021
grains{b}=sum(x_array{b}>ymin) && (x_array{b}<ymax)
Your sum() ends too early. sum(condition & condition)

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by