How to get matlab to automatically read csv data with a manual input?

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)

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

Hi Walter,
Thanks for the reply.
This is an abstract of what i have. The complete code is much longer.
m = csvread ('TestDataSheet.csv');
for loop_m = 36:14
val1 = m(:,1); %%Age in years
val2 = m(:,2); %%Gender
val3 = m(:,3); %%Chest pain type
val4 = m(:,4); %%Resting blood pressure
val5 = m(:,5); %%Serum cholesterol in mg/dl
val6 = m (:,6); %%Fasting blood sugar
val7 = m (:,7); %%Resting electrocardiographic results
val10 = m (:,10); %%ST depression induced by exercise relative to reset
val11 = m (:,11); %%The slope of the peak exercise ST segment
val12 = m (:,12); %%Number of major vessels coloured by flourosopy
val13 = m (:,13); %%Heart condition
if val3 (n) <= 3 && val13 (n) <= 3 && val10 (n) <= 1.6 % top of tree, down far left hand side to far left value
result = 'F(86.69/4.0)';
display (result)
elseif val3 (n) > 3 && val12 (n) > 0
result = 'T(69.54/4.0)';
display (result)
end
end
Can you tell for that where i have went wrong. Again i appreciate the help, I am quite new to matlab.
that turned out completely wrong, ovbiously it is layed out correctly.
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{:});
Hi Walter, Im getting this error;
Index exceeds matrix dimensions.
Error in boo (line 32) val1 = m(loop_m,1);
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.)
Hi Walter, What i want to do is use my code to determine true or false form my csv. data that i have input as 'm'. This data has 36 columns and 13 rows. Each row has a heading, e.g Val1, Val2 up to Val 13. I have removed these headings as I know matlab struggles with text from csv. files. The 14th row has the true or false outcome. I have replaced these with 1's and 0's. So what i want matlab to do is read each column individually and automatically input the data into my code and in turn output 36 individual pieces of information at once e.g 1 or 0. Thanks for you time Walter. Its much appreciated. I'd like to add that my code has 4 if statements and a number of elseifs embedded in these if statements.
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.
Hi Walter, My apologies, all the data for the patients are in rows. The columns have the headers val1, val2 etc which i have removed. Then the 14th column is the 1 or 0 (true or false). So i have done all of the above. I am getting no errors but i am also getting no output.
% right half of tree (right hand side)
if val3 > 3 && val12 <= 0 && val13 > 6 && val11 <= 1 && val10 <= 0.6 && val1 <= 42
result{row_idx} = 'T(3.0)';
elseif val3 > 3 && val12 <= 0 && val13 > 6 && val11 <= 1 && val10 <= 0.6 && val1 > 42
result{row_idx} = 'F(4.0)';
elseif val3 > 3 && val12 <= 0 && val13 > 6 && val11 <= 1 && val10 >0.6
result{row_idx} = 'T(3.0)';
elseif val3 > 3 && val12 <= 0 && val13 > 6 && val11 > 1
result{row_idx} = 'T(13.86)';
elseif val3 > 3 && val12 > 0
result{row_idx} = 'T(69.54/4.0)';
end
end
end
end
end
fprintf('%s\n', result{:});
% that is the last 1/4 of my code
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.
Hi Walter,
Could I ask you to explain that in a bit more detail. I added the 'else' case to -> else result = ('Unconfirmed');

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 27 Oct 2016

Edited:

on 28 Oct 2016

Community Treasure Hunt

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

Start Hunting!