Borrar filtros
Borrar filtros

Hi! How can I create a conditional statement from checking if an array is empty? I will paste my non-working code here.

2 visualizaciones (últimos 30 días)
TF = isempty(matches); %true-false statement: logical 1 = True, logical 0 = False
GRRRRR = double(TF);
if GRRRRR == 1 %%if it is true that matches is empty
fprintf['No match has been found throughout whole run'];
[SL: formatted code as code]

Respuestas (2)

Steven Lord
Steven Lord el 13 de Oct. de 2022
You don't actually have to convert the logical value into double and compare it to 1. From the documentation for the if keyword: "if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true." The only problems I see with your code is that you're not using parentheses to call fprintf and that you didn't end the if block with an end statement (though that latter problem could be that you just didn't copy and paste it from your code.)
x = [];
if isempty(x)
fprintf('The x array is empty');
else
fprintf('The x array is not empty');
end
The x array is empty

Hanojhan Rajahrajasingh
Hanojhan Rajahrajasingh el 13 de Oct. de 2022
A = zeros(0,2,2);
TF = isempty(A);
GRRRRR = double(TF);
if GRRRRR == 1
fprintf('No match has been found throughout whole run');
else
fprintf('Match has been found');
end
No match has been found throughout whole run

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by