필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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);
end
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');
end
%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});
end
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
figure(1)
bar(time,grains, 0.01, 'EdgeColor', 'r')
title('mass efflux')
xlabel('Time (S)')
ylabel('Mass Efflux (No. of Particles)')

채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 20일
grains{b}=sum(x_array{b}>ymin) && (x_array{b}<ymax)
Your sum() ends too early. sum(condition & condition)
  댓글 수: 5

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by