Running csv file through if else statement

My aim is to have code that will run a csv file through an if statement. I have got it to extract the correct information from the file
I have been using the following code:
fid = fopen('eyes.csv');
data = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %*s','Delimiter',',','Headerlines',1);
fclose(fid);
numrows = size(data,1);
for rownum = 1:numrows
thisrow = data(rownum,:);
val1 = thisrow{1};
val2 = thisrow{2};
val3 = thisrow{3};
val4 = thisrow{4};
val5 = thisrow{5};
val7 = thisrow{7};
val8 = thisrow{8};
val10 = thisrow{10};
val11 = thisrow{11};
val12 = thisrow{12};
val13 = thisrow{13};
if val3<=3 && val10<=1.6 && val1<=56
outcome = 'F';
elseif val3<=3 && val10<=1.6 && val1>56 && val2<=0
outcome = 'F';
elseif val3<=3 && val10<=1.6 && val1>56 && val2>0 && val7<=1
outcome = 'F';
etc.
However I get the error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in Untitled (line 18)
if val3<=3 && val10<=1.6 && val1<=56
I don't understand becuase all the data for each variable 3, 1.4, 67 etc. When I change && to &, it only provides me with one result instead of giving me answers to all the cases in the file.
Any advice? Thanks

Respuestas (1)

Star Strider
Star Strider el 14 de Nov. de 2018

0 votos

First, if you are comparing vectors,use one ‘&’, not ‘&&’.
Second, again if you are comparing vectors, you will likely need the any or the related all function, depending on what you are doing.

5 comentarios

Peggatha
Peggatha el 14 de Nov. de 2018
Editada: Peggatha el 14 de Nov. de 2018
I am not comparing vectors, just values and as I said in the question, when I change to & it only provides me with the answer to the first line of data. It should be giving me answers to all the data ie 50 cases in the file.
It seems to me that from the way you imported your file, you are comparing vectors, since each ‘val#’ vector is a row of your ‘data’ matrix.
Your code is doing something like this:
val1 = rand(1,10)
val2 = rand(1,10)
test1 = val1 < 0.7
test2 = val2 > 0.3
logical_compare = (val1 < 0.7) & (val2 > 0.3)
that evaluated will produce for example:
val1 =
0.6557 0.0357 0.8491 0.9340 0.6787 0.7577 0.7431 0.3922 0.6555 0.1712
val2 =
0.7060 0.0318 0.2769 0.0462 0.0971 0.8235 0.6948 0.3171 0.9502 0.0344
test1 =
1×10 logical array
1 1 0 0 1 0 0 1 1 1
test2 =
1×10 logical array
1 0 0 0 0 1 1 1 1 0
logical_compare =
1×10 logical array
1 0 0 0 0 0 0 1 1 0
Yolu need to examine the results of the expressions you are evaluating so you can be certain your code is doing what you want it to.
Peggatha
Peggatha el 14 de Nov. de 2018
Editada: Peggatha el 14 de Nov. de 2018
For example, val1 is age, val2 is weight etc and I want these different variables to run through the if statement to determine if each person has a certain disease or not.
Val1...val13 are the columns in the csv file. I want the programme to run each row ie each patient data.
Sorry for getting confused. If I don't want to import it as an array, how do I stop it?
I cannot accurately reproduce the problem you are having without your file.
When I emulate it (as best I can otherwise), you actually are comparing vectors, so you need to decide how you want to do it.
Example —
data = num2cell(randi(9,10));
numrows = size(data,1);
for rownum = 1:numrows
thisrow = data(rownum,:);
val1 = thisrow{1};
val2 = thisrow{2};
L(rownum) = (val1 < 5) & (val2 > 5);
end
all_v = all(L)
any_v = any(L)
Sorry if I'm being confusing, I'm new to matlab so my apologies.
This code worked for me:
M = csvread('test.csv',1,0)
numrows = size(M,1)
for rownum = 1:numrows
thisrow = M(rownum,:);
val1 = thisrow(1);
val2 = thisrow(2);
val3 = thisrow(3);
val4 = thisrow(4);
val5 = thisrow(5);
val7 = thisrow(7);
val8 = thisrow(8);
val10 = thisrow(10);
val11 = thisrow(11);
val12 = thisrow(12);
val13 = thisrow(13);
if val3<=3 && val10<=1.6 && val1<=56
outcome = 'F'
elseif val3<=3 && val10<=1.6 && val1>56 && val2<=0
outcome = 'F'
......
etc
However when I bring in this new file in which needs the first row and last column excluded, I changed the code to the code in the first question and now it doesn't work as for some reason it is viewing the columns as arrays now but before it didn't.
I need it so someone could potentially just change the file name (same columns but different number of rows) and will give them results by going through the if statement?
Sorry if you have addressed this already and I'm not understanding.

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Preguntada:

el 14 de Nov. de 2018

Comentada:

el 14 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by