Running csv file through if else statement
Mostrar comentarios más antiguos
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
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
Star Strider
el 14 de Nov. de 2018
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.
Star Strider
el 14 de Nov. de 2018
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)
Peggatha
el 14 de Nov. de 2018
Categorías
Más información sobre File Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!