invalid use of operator for matlab loop?

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;
Invalid use of operator.
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

Please attach your xls file using the paperclip icon, and share the complete error message (all the red text)
% rename the variable as T or something other,
TT = readtable('Final_COVID_Data (2).xls')
You need to rename the variable table in the above line to some other name as it conflicts with Matlab standard function table.
The same name table is used in the below line
T = table(id, age, diagnosis);
also this definition of variables in the below lines is not allowed in Matlab
youngage_count = <=25;
middleage_count = 25>65;
oldage_count = =>65 ;
It seems there is also an extra end keyword in your program.
TT=readtable('Final_COVID_Data (2).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
% 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 < 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);
Wong file. Please attach 'Final_COVID_Data (2).xls'
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')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
TT = 99×3 table
ID Age COVIDStatus __________ ___ ___________ 1.5031e+08 18 0 1.5031e+08 52 0 1.5031e+08 35 0 1.5031e+08 78 1 1.5031e+08 43 1 1.5031e+08 22 0 1.5031e+08 65 1 1.5031e+08 35 1 1.5031e+08 78 1 1.5031e+08 89 1 1.5031e+08 15 0 1.5031e+08 16 0 1.5031e+08 17 1 1.5031e+08 87 1 1.5031e+08 65 1 1.5031e+08 43 0
%group1=age <=25%
%group2=age 25>65%
%group3=age =>65%
PositiveCOVIDStatus=1
PositiveCOVIDStatus = 1
NegativeCOVIDStatus=0
NegativeCOVIDStatus = 0
%Load the data from the .csv file
data = readtable('Final_COVID_Data (1).xls')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
data = 99×3 table
ID Age COVIDStatus __________ ___ ___________ 1.5031e+08 18 0 1.5031e+08 52 0 1.5031e+08 35 0 1.5031e+08 78 1 1.5031e+08 43 1 1.5031e+08 22 0 1.5031e+08 65 1 1.5031e+08 35 1 1.5031e+08 78 1 1.5031e+08 89 1 1.5031e+08 15 0 1.5031e+08 16 0 1.5031e+08 17 1 1.5031e+08 87 1 1.5031e+08 65 1 1.5031e+08 43 0
%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
Error using {}
Unrecognized table variable name 'Test_Result'.
% 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);

Sign in to comment.

 Accepted Answer

This works:
% Load the data from the .xlsx file
data = readtable('Final_COVID_Data (1).xls');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
% 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);
Young aged positive test results: 7
fprintf('Middle aged positive tes results: %d\n', middleage_count)
Middle aged positive tes results: 20
fprintf('Old aged positive test results: %d\n', oldage_count);
Old aged positive test results: 25

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Tags

Asked:

Nma
on 2 May 2023

Moved:

on 2 May 2023

Community Treasure Hunt

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

Start Hunting!