invalid use of operator for matlab loop?
4 views (last 30 days)
Show older comments
I am trying to get the table to read positive diagnoses and sort them into the correct age group
table=readtable('Final_COVID_Data (1).xls')
%group1=age <=25%
%group2=age 25>65%
%group3=age =>65%
PositiveCOVIDStatus=1
NegativeCOVIDStatus=0
%Load the data from the .csv file
data = readtable('Final_COVID_Data (2).xls')
%Initialize counters for each age group
youngage_count = <=25;
middleage_count = 25>65;
oldage_count = =>65 ;
%Loop through rows of the data table
for i = 1:height(data)
% Check if test result is positive
if data{i, 'Test_Result'} == 'positive'
%Get the patien'ts age
age = data{i, 'Age'};
% Sort the patient into their appropriate age group
if age<=25
youngage_count = youngage_count + 1;
elseif age < 65
middleage_count = middleage_count + 1;
else
oldage_count = oldage_count + 1;
T = table(id, age, diagnosis);
mask = diagnosis == "positive";
subset = T(mask,:);
end
end
end
end
% Print out the total number of positive test
results in each age group
fprintf('Young aged positv test results: %d\n', youngage_count);
fprintf('Middle aged positive tes results: %d\n', middleage_count)
fprintf('Old aged positive test results: %d\n', oldage_count);
6 Comments
VBBV
on 2 May 2023
Moved: VBBV
on 2 May 2023
You can see from below that file contains no variable named Test_result . The only variables in file are ID, Age COVIDStatus. It must be incorrect file
TT=readtable('Final_COVID_Data (1).xls')
%group1=age <=25%
%group2=age 25>65%
%group3=age =>65%
PositiveCOVIDStatus=1
NegativeCOVIDStatus=0
%Load the data from the .csv file
data = readtable('Final_COVID_Data (1).xls')
%Initialize counters for each age group
% define a values for these variables
youngage_count = 25;
middleage_count = 25:65; % range here
oldage_count = 65 ;
%Loop through rows of the data table
for i = 1:height(data)
% Check if test result is positive
if data{i, 'Test_Result'} == 'positive'
%Get the patien'ts age
age = data{i, 'Age'};
% Sort the patient into their appropriate age group
if age<=25
youngage_count = youngage_count + 1;
elseif age > 25 & age < 65
middleage_count = middleage_count + 1;
else
oldage_count = oldage_count + 1;
T = table(id, age, diagnosis);
mask = diagnosis == "positive";
subset = T(mask,:);
end
end
end
% extra end
%end
% Print out the total number of positive test results in each age group
fprintf('Young aged positv test results: %d\n', youngage_count);
fprintf('Middle aged positive tes results: %d\n', middleage_count)
fprintf('Old aged positive test results: %d\n', oldage_count);
Accepted Answer
Image Analyst
on 2 May 2023
This works:
% Load the data from the .xlsx file
data = readtable('Final_COVID_Data (1).xls');
% Initialize count values for each age range.
youngage_count = 0;
middleage_count = 0; % range here
oldage_count = 0;
% Loop through rows of the data table
for k = 1 : height(data)
% Check if test result is positive
if data.COVIDStatus(k) == 1
% Get the patient's age
age = data.Age(k);
% Sort the patient into their appropriate age group
if age <= 25
youngage_count = youngage_count + 1;
elseif age < 65
middleage_count = middleage_count + 1;
else
oldage_count = oldage_count + 1;
end
end
end
% Print out the total number of positive test results in each age group
fprintf('Young aged positive test results: %d\n', youngage_count);
fprintf('Middle aged positive tes results: %d\n', middleage_count)
fprintf('Old aged positive test results: %d\n', oldage_count);
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!