Error:Conversion to cell from logical is not possible.

28 views (last 30 days)
Dear programmers
I want to study a logical algorithm. I have some values in a matrix(n x 3) where the first column is serial number and last 2 columns are variable values and number of rows is "n" . The variables are HP and SS. I want to execute logical statement upon these values under variables HP and SS to get a desired result and store the results for each row in a separate columns say, Column E and Column V . I dont know how to store the values from if else statements in separate column. While executing the below code with "disp" syntax also I am receiving an err message as " Conversion to cell from logical is not possible".
I am sharing the code. Please check and help.
if {(HP==0)&& (SS==0)}
E =1;
V = 0.167;
elseif {(HP > 0.15)||((HP > -0.15 )&& (SS > 0.05))||((HP > -0.15 )&& (SS < -0.05))}
E = 2;
V = 0.167;
elseif {((HP < -0.15) && (SS > 0.15 ))||(( HP < -0.15) && (SS < -0.15 ))}
E = 10;
V = 0.167;
elseif {(HP < -0.15) && (-0.15 < SS < 0.15 )}
E = 1000;
V = 0.3;
elseif {( -0.15 < HP < 0.15) && ( -5 < SS < 5)}
E = 6000;
V = 0.3;
end
  9 Comments
madhan ravi
madhan ravi on 2 May 2019
@Ahmad: Why did you delete the comment which I was referring to?
jahanzaib ahmad
jahanzaib ahmad on 2 May 2019
Edited: jahanzaib ahmad on 2 May 2019
i thought i made a mistake .. i never used tables .
"its a table .. so { } should not be used ..."

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 2 May 2019
Edited: Guillaume on 2 May 2019
It's difficult to follow a conversation when some comments have been deleted... I cannot see any table in any of the code that has been posted.
Anyway, one thing for sure,
if {any_expression}
has never been valid syntax in matlab and will indeed result in the error mentioned in the title of the question. The {} are not valid here. While in maths, you can use {} as brackets similar to (), in matlab it means something completely different. The correct syntax is
if any_expression
Making up your own syntax is not going to be very succesful. You need to read the documentation and use the exact same syntax that is stated. Another made up syntax is
-0.15 < SS < 0.15
While this will not throw an error, it will certainly not produce the result desired. The above will evaluate first -0.15 < SS which will produce a true (1) result or false (0) result. It will then compare that 0 or 1 with 0.15. The end result is that the expression produces a true result for any SS <= -0.15 and false otherwise. Not what was intended at all!
The correct expression is:
-0.15 < SS && SS < 0.15
Also note that
if (HP > 0.15)||((HP > -0.15 )&& (SS > 0.05))||((HP > -0.15 )&& (SS < -0.05))
reduces to simply:
if HP > 0.15
Finally, note that the preallocation of E and V should be done outside the loop and since E and V are meant to contain numbers they shouldn't be allocated as logical but as double. So, code with all the mistakes removed:
E = zeros(size(m, 1), 1); %preallocation done the right way
V = zeros(size(m, 1), 1);
for i= 1:size(m, 1) %don't hardcode size of arrays (with you n = 1000). Ask matlab for it, so that if the array size changes you don't have to do a thing
HP = m(i,1);
SS = m(i,2);
if HP==0 && SS==0
E(i,1) = 1;
V(i,1) = 0.167;
elseif HP > 0.15
E(i,1) = 2;
V(i,1) = 0.167;
elseif HP < -0.15 %splitting the expression for clarity
if SS < -0.15 || SS > -0.15
E(i,1) = 10;
V(i,1) = 0.167;
else
E(i,1) = 1000;
V(i,1) = 0.3;
end
elseif HP > -0.15 && HP < 0.15 && SS > -5 && SS < 5
E(i,1) = 6000;
V(i,1) = 0.3;
else
warning('current values of HP and SS not covered by the tests')
end
end
Note that as with your original code, the cases where
  • HP == -0.15 || HP == 0.15
  • HP is not between -0.15 and 0.15 and SS is greater than 5 or smaller than -5
are not covered by any of the tests, So E an S will stay 0. At least I'm warning you in these cases.
Unlike your original code, the case where HP is <-0.15 and SS is either -0.15 or 0.15 is covered by my tests. It's considered the same as SS being between -0.15 and 0.15.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!