MATLAB: How to check convertibility to logical values that do not raise an error in the if statement

conversionif statementlogical?test

Is there a function to check the convertibility to logical values that do not raise an error in the if statement? For example,
if struct()
if {}
will cause errors. I know I can check by:
function isLS = islogicalable(x)
isLS = false;
try
if x
end
isLS = true;
end
But is there a function for checking this? Or is it true that only numeric, logical and char can be converted to logical values?

Best Answer

Numerical, logicals and char scalars can be converted to a logcial.
function isLS = islogicalable(x)
isLS = (numel(x) == 1 & (ischar(x) || isnumeric(x) || islogical(x) || isinteger(x)));
end
Note that if performs an implicite conversion, if the argument is not a scalar:
if all(cond(:)) && ~isempty(cond)
Care about dpb's comment: I cannot imagine a situation in which this is required or useful. The class of the variables should be known in a clean code - where "clean" means free of eval and assignin. A user-defined function might reply different values, e.g.:
function R = myCode
if rand > 0.5
R = struct([]);
else
R = 17;
end
But this would be at least confusing. In such a case I'd prefer to fix this function, such that there is no need to check if the output can be converted to a logical.