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');

1 件のコメント

dpb
dpb 2020 年 12 月 7 日
編集済み: dpb 2020 年 12 月 7 日
Please return to the original Q? that Walter gave guidance for --
What Walter suggested was to turn your code into a function named thisthrow and pass the input data to it by row.
The note by Harry is definitely true; looks like you may intend some ORs in there instead of all ANDs. On going back to original, I see the same logic therein; just the name change appears to be all.
I noticed it but hadn't tried to make sense out of it as I was trying to see about building the logic tree...it could be possible to first select for <=3 and then subset that to <=1, but not with the logic as written as AND--those that are <=1 are also <=3 so both are true which leads, I suspect, to a result not what is intended with the compound comparison.
Also, you want '&' here, not '&&'

サインインしてコメントする。

 採用された回答

Harry Laing
Harry Laing 2020 年 12 月 7 日

0 投票

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?

9 件のコメント

Ronan Rafferty
Ronan Rafferty 2020 年 12 月 8 日
Yeah, I've seen that some of the code is pointless, it was given to us as a rough decision tree and even though it's flawed we are supposed to use it and give feedback on it later as part of my assignment. I've checked and they've said not to worry about some of the pointless conditions.
I have altered the decision tree as you recommended and tried using that code and it is coming back with an error message.
The message coming up is:
Error using tabular/dotParenReference (line 69)
Unrecognized variable name 'Chest_Pain_Type'.
Error in BME501_CourseworkPartA_Ronan_Rafferty_B00748725 (line 17)
if 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
For reference, line 69 is:
elseif myData.Chest_Pain_Type(i) >3 && myData.Serum_Cholesterol(i) >0 && myData.ST_Depression(i) >0.8 && myData.Gender(i) <=0 && myData.Heart_Condition(i) >3
Patient0utput = 1;
It is the last elseif statement before the else and end of that function.
Would you know how to fix this error?
dpb
dpb 2020 年 12 月 8 日
Somewhere you have to define all the variables you reference -- the data is brought in as myData but then you reference a whole bunch of other variables such as this one; they have to have been created somewhere first before can use them.
The syntax myData.Chest_Pain_Type(i) is apparently being interpreted based on the error message as a table variable but your original code using readmatrix doesn't return a table but an array.
So, all we have to look at is disparate pieces of stuff that clearly don't go together.
We need a complete consistent set of code; we can't tell what you've done besides what is posted; we can't see your terminal from here.
What does
whos myData
return?
Harry Laing
Harry Laing 2020 年 12 月 8 日
Exactly what dpb says. I was assuming your csv file has headings of the exact same name (i.e. the headings are Chest_Pain_Type, etc, with underscores). Using readtable would then create a table variable with headings of the same name. If the heading names are different in the csv file, you should either change your variable names or change the code to be consistent with the actual variable names that are created.
If you are using readmatrix then you will not have a table variable, and so using the 'dot' referenceing will give you an error.
Ronan Rafferty
Ronan Rafferty 2020 年 12 月 8 日
My bad, I had changed the table headings in the csv file but had forgot to save the file before running the matlab code hahaha!
I've ran the code and the batch read works now, thanks so much for your help!
Ronan Rafferty
Ronan Rafferty 2020 年 12 月 9 日
I seem to have come on another problem, could be me being stupid again!
I have gotten the batch read function to work perfectly but for some reason my manual input option isn't working correclty as it was before?
The error message I am seeing is:-
Operands to the || and && operators must be convertible to logical
scalar values.
Error in BME501_CourseworkPartA_Ronan_Rafferty_B00748725 (line 136)
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 &&
Serum_Cholesterol >0 && Gender >0 && Resting_ECG >0 && Resting_ECG
>1 && Fluoroscopy_Vessels <=0 && Serum_Cholesterol <=271
I have tested it and some of the other branches are working okay, it just seems that sometimes this error message comes up?
dpb
dpb 2020 年 12 月 9 日
As noted before, unless you pass only a single element you needed "&" for array operation instead of "&&". Seems to have gone unheeded.
& Element-wise Logical AND.
A & B is an array whose elements are logical 1 (TRUE) where both A
and B have non-zero elements, and logical 0 (FALSE) where either has
a zero element. A and B must have compatible sizes. In the simplest
cases, they can be the same size or one can be a scalar. Two inputs
have compatible sizes if, for every dimension, the dimension sizes of
the inputs are either the same or one of them is 1.
&& Short-Circuit Logical AND.
A && B is a scalar value that is the logical AND of scalar A and B.
This is a "short-circuit" operation in that MATLAB evaluates B only
if the result is not fully determined by A. For example, if A equals
0, then the entire expression evaluates to logical 0 (FALSE), regard-
less of the value of B. Under these circumstances, there is no need
to evaluate B because the result is already known.
Ronan Rafferty
Ronan Rafferty 2020 年 12 月 9 日
Apologies, I don't quite understand? Does this mean i need to change all of the &&'s to & in the entire decision tree or just this line?
Walter Roberson
Walter Roberson 2020 年 12 月 9 日
Any time you have an "if" that is testing a vector you probably want to turn it into logical indexing instead.
Harry Laing
Harry Laing 2020 年 12 月 9 日
To quote your error:
Error in BME501_CourseworkPartA_Ronan_Rafferty_B00748725 (line 136)
elseif Chest_Pain_Type <=3 && Induced_Angina <=0 && Age >55 &&
Serum_Cholesterol >0 && Gender >0 && Resting_ECG >0 && Resting_ECG
>1 && Fluoroscopy_Vessels <=0 && Serum_Cholesterol <=271
In the error you have, you appear to be referring to a variable "Chest_Pain_Type" (and so on). Is this variable a single value (scalar) or does it contain multiple values (a vector or matrix)? Are you intending it to be a vector of values or just a single value? Same question for every variable name. Run the script with breakpoints to debug the code and confirm this for yourself.
If it is a vector of all the data, then using && wont work because && requires scalar values either side. I believe you are intending for each variable to be a single value, in which case using && should still work. Though as walter said, logical indexing would make sense here.

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2020 年 12 月 7 日

0 投票

MATLAB is case sensitive--the function is readmatrix, not readMatrix

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by