How to get matlab to automatically read csv data with a manual input?
Show older comments
I have a piece of code written that analyses (numerical) patient data e.g. age. I am using an if statement with a number of sub elseif statements to generate an output. The data to be analysed will ultimately produce a False / Positive output. The code is working but i want matlab to read the data automatically. I have used csvread and am attempting to introduce a for loop but this is where i get stuck. Any help would be grateful.
Answers (1)
Walter Roberson
on 27 Oct 2016
Example:
num = csvread('YourFile.txt');
numrows = size(num, 1);
ticket_prices = zeros(numrow, 1); %the output variable
for rownum = 1 : numrows
thisrow = num(rownum, :);
age = thisrow(7); %for example, if the age were in column 7 of the row
if age < 3
ticket_price = 0;
elseif age <= 5
ticket_price = 4.99;
elseif age <= 13
ticket_price = 10.99;
elseif age <= 18
ticket_price = 15.84;
else
ticket_price = 206.18;
end
ticket_prices(thisrow) = ticket_price;
end
13 Comments
brian donnelly
on 27 Oct 2016
Edited: Walter Roberson
on 27 Oct 2016
brian donnelly
on 27 Oct 2016
Walter Roberson
on 27 Oct 2016
m = csvread ('TestDataSheet.csv');
rows_to_examine = 36 : 36 + 14 - 1; %14 rows
num_rows = length(rows_to_examine);
result = cell(num_rows, 1);
for row_idx = 1 : num_rows
loop_m = rows_to_examine(row_idx);
val1 = m(loop_m,1); %%Age in years
val2 = m(loop_m,2); %%Gender
val3 = m(loop_m,3); %%Chest pain type
val4 = m(loop_m,4); %%Resting blood pressure
val5 = m(loop_m,5); %%Serum cholesterol in mg/dl
val6 = m(loop_m,6); %%Fasting blood sugar
val7 = m(loop_m,7); %%Resting electrocardiographic results
val10 = m(loop_m,10); %%ST depression induced by exercise relative to reset
val11 = m(loop_m,11); %%The slope of the peak exercise ST segment
val12 = m(loop_m,12); %%Number of major vessels coloured by flourosopy
val13 = m(loop_m,13); %%Heart condition
if val3 <= 3 && val13 <= 3 && val10 <= 1.6 % top of tree, down far left hand side to far left value
result{row_idx} = 'F(86.69/4.0)';
elseif val3 > 3 && val12 > 0
result{row_idx} = 'T(69.54/4.0)';
else
result{row_idx} = '???';
end
end
fprintf('%s\n', result{:});
brian donnelly
on 27 Oct 2016
brian donnelly
on 27 Oct 2016
Walter Roberson
on 27 Oct 2016
Your syntax
for loop_m = 36:14
was ambiguous. I interpreted it as meaning that you wanted to start at 36 and go for another 14 further on. If you instead wanted to start at 36 and count backwards to 14 then use
rows_to_examine = 36 : -1 : 14;
Nothing else needs to be changed (but the output would be in that order, for 36 first, then 35, and so on, which would tend to confuse people.)
brian donnelly
on 27 Oct 2016
Edited: brian donnelly
on 27 Oct 2016
Walter Roberson
on 27 Oct 2016
One of the two of us is confused about rows and columns.
val3 = m(loop_m,3); %%Chest pain type
would be looking at column 3 of a row.
Is your data arranged so that all of the information for one patient is on a row, or is your data arranged so that all of the information for a patient is on a column?
If the information for one patient is all in one row, then change to
rows_to_examine = 1 : 36;
If the information for one patient is all in one column then we need to rewrite the code.
brian donnelly
on 27 Oct 2016
brian donnelly
on 27 Oct 2016
Walter Roberson
on 27 Oct 2016
You do not have a final 'else' case for the possibility that none of the conditions hold.
Anyhow, time for you to use the debugger. Put a breakpoint on the first line of the for loop and start stepping through.
brian donnelly
on 27 Oct 2016
Walter Roberson
on 28 Oct 2016
Edited: Walter Roberson
on 28 Oct 2016
Categories
Find more on Loops and Conditional Statements 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!