invalid use of operator for matlab loop?
Afficher commentaires plus anciens
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 commentaires
Cris LaPierre
le 2 Mai 2023
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);
Image Analyst
le 2 Mai 2023
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')
%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);
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!