MATLAB: ReadMatrix/for loop issue

for loop

I'm having issues with reading in data and using the for loop. I need the code to read in 90 rows of 13 different parameters of data from a .csv file and understand that each row is one set of data and assign the correct variable to the correct value so that it can then use this to run the decision tree and give me an outcome for each row.
I am using Matlab R2018b version.
This is the piece of code I am using
myData = readmatrix('BME501_Coursework_Testdata.csv');
for i = 1 : size(myData,1)
thisrow = myData(i,:);
if Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender <=0
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender >0 && Resting_ECG <=1 && Chest_Pain_Type <=46
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender >0 && Resting_ECG <=1 && Chest_Pain_Type >46
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type <=1 && Gender >0 && Resting_ECG >1
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age <=55 && Chest_Pain_Type >1
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG <=0
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender <=0
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type <=1
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type >1 && Resting_BP <=128 && Max_HR <=142
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type >1 && Resting_BP <=128 && Max_HR >142
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG <=0 && Chest_Pain_Type >1 && Resting_BP >128
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG <=1
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG >1 && Fluoroscopy_Vessels <=0 && Resting_ECG <=271
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG >1 && Fluoroscopy_Vessels <=0 && Resting_ECG >271
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 && Resting_ECG >0 && Gender >0 && Resting_ECG >0 && Resting_ECG >1 && Fluoroscopy_Vessels >0
Patient0utput = 1;
elseif Chest_Pain_Type <=3 && Induced_Angina >0 && ST_Slope <=1
Patient0utput = 0;
elseif Chest_Pain_Type <=3 && Induced_Angina >0 && ST_Slope >1
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol <=0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender <=0 && Induced_Angina <=0
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender <=0 && Induced_Angina >0 && Resting_ECG <=0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender <=0 && Induced_Angina >0 && Resting_ECG >0
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender >0 && Fluoroscopy_Vessels <=0 && Heart_Condition <=3
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender >0 && Fluoroscopy_Vessels <=0 && Heart_Condition >3
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression <=0.8 && Gender >0 && Fluoroscopy_Vessels >0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression >0.8 && Gender <=0 && Heart_Condition <=3 && Induced_Angina <=0
Patient0utput = 0;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression >0.8 && Gender <=0 && Heart_Condition <=3 && Induced_Angina >0
Patient0utput = 1;
elseif Chest_Pain_Type >3 && Serum_Cholesterol >0 && ST_Depression >0.8 && Gender <=0 && Heart_Condition >3
Patient0utput = 1;
else
Patient0utput = 1;
end
if Patient0utput <1
disp 'This patient does not have cardiovascular disease';
else
disp 'This patient has cardiovascular disease';
end
end
I was told to use thisrow in the decision tree but where do I put this in?
This is the error that comes up when i go to run the code
Undefined function or variable 'readMatrix'.
Error in Testing (line 1)
myData = readMatrix('BME501_Coursework_Testdata.csv');

Best Answer

  • I've never heard of the function readMatrix. I'd suggest using the function readtable if your CSV file has headings as well as data. (This answer here on importing CSV data may also help). You also need to refer to the column (or table heading) and row number in your if conditions.
    In the example below, I assume your csv file has the same heading names you used in your if statemets:
    myData = readtable('myfile.csv');
    [numRows, numCols] = size(myData);
    for i = 1:NumRows
    if myData.Chest_Pain_Type(i) <=3 && myData.Induced_Angina(i) <=0 && myData.Age(i) <=55 && myData.Chest_Pain_Type(i) <=1 && GmyData.Gender(i) <=0
    Patient0utput = 0;
    elseif myData.Chest_Pain_Type(i) <=3 && myData.Induced_Angina(i) <=0 && myData.Age(i) <=55 && myData.Chest_Pain_Type(i) <=1 && myData.Gender(i) >0 && myData.Resting_ECG(i) <=1 && myData.Chest_Pain_Type(i) <=46
    Patient0utput = 1;
    % etc etc etc
    end
    Note: Although, I must say, you seem the have mutliple statements that are useless in some of your if conditions. For example, it seems pointless to me to have both
    Chest_Pain_Type <= 3
    and
    Chest_Pain_Type <= 1
    because if the value is less than or equal to 1, then the condition of being less than or equal to 3 is useless?