Matlab Online won't update logical variable in the workspace.

Just encountered this issue in the following example. When I run this code in MATLAB Online, the result is correct, so I know that the value of is_prime is updated correctly during runtime. However, when I check the value of is_prime in the workspace, it does not get updated. For example, when the condition rem(n, number) == 0 is satisfied, is_prime should become false, but in the workspace, it still shows true. Interestingly, when I double-click on the value, it displays the correct value in a separate window. Is there a reason why this happens? The other variables are updated correctly.
n = 6;
is_prime = true; % flag variable
for number = 2:n-1
% check if the value n is divisbile by the index "number"
if rem(n,number) == 0
is_prime = false;
fprintf("%d is not a prime number! divisible by %d\n",n,number)
end
end
6 is not a prime number! divisible by 2 6 is not a prime number! divisible by 3
is_prime
is_prime = logical
0
if is_prime
fprintf("Prime number")
else
fprintf("Not a prime number")
end
Not a prime number

2 Comments

I tested out in MATLAB R2024b desktop, and I tested out in MATLAB Online. I am not able to replicate the variable showing up as true.
I tried this in the desktop version of R2024b like Walter did. Like Walter I was not able to reproduce this behavior, either with the display of the variable in the Command Window or with the display in the Workspace component of the Desktop.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2024b

Asked:

on 17 Sep 2024

Commented:

on 17 Sep 2024

Community Treasure Hunt

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

Start Hunting!