Nested If-ELSEIF statements

11 visualizzazioni (ultimi 30 giorni)
Hans123
Hans123 il 16 Giu 2020
Commentato: Hans123 il 16 Giu 2020
I tried looking up this question in litreature and the forum and I couldn't find anything pertaining to my situation.
I have the following nested if-elseif statements, and I observed by using breakpoints, the code skips after processing the first if-elseif statement. It skips the caulations at the ellipsis too. I want it to run through the rest of the elseif and check the logic and calculate results - any help is appreciated
This excerpt is supposed to change the extreme points out of Nx and -Nx.
if X<-Nx
X=Nx;
elseif X>Nx
X=-Nx;
if Y<-Ny
Y=Ny;
elseif Y>Ny
Y=-Ny;
if Z<-Nz
Z=Nz;
elseif Z>Nz
Z=-Nz;
...
end
end
end
  3 Commenti
Hans123
Hans123 il 16 Giu 2020
I think I figured out the issue, the reason the code skipped the rest of the if-elseif is because my end commands were clustered together at the end of the code. I should have end my if-elseif statement right afterwards before starting a new one.
Thanks^
FIX
if X<-Nx
X=Nx;
elseif X>Nx
X=-Nx;
end
if Y<-Ny
Y=Ny;
elseif Y>Ny
Y=-Ny;
end
if Z<-Nz
Z=Nz;
elseif Z>Nz
Z=-Nz;
end
...
Hans123
Hans123 il 16 Giu 2020
@Bjorn, thank you for your reply - this is meant for a periodic boundary condtion statement. It is not intuitive took me a minute too

Accedi per commentare.

Risposta accettata

Geoff Hayes
Geoff Hayes il 16 Giu 2020
Hans123 - do you really need nested if-elseif blocks to change the extreme points? Wouldn't you just do them for x, y, and z seperately? Otherwise you might see this "skipping" depending upon which condition evaluates to true. For example,
if X<-Nx
X=Nx;
elseif X>Nx
X=-Nx;
if Y<-Ny
Y=Ny;
elseif Y>Ny
% etc.
if the first condition evaluates to true i.e. X < -Nx then the code to check Y and Z will not be evaluated since they are nested within the elseif body. Instead try
if X<-Nx
X=Nx;
elseif X>Nx
X=-Nx;
end
if Y<-Ny
Y=Ny;
elseif Y>Ny
Y=-Ny;
end
if Z<-Nz
Z=Nz;
elseif Z>Nz
Z=-Nz;
end
Note how the above code is duplicated for x, y, and z. This might be okay for just three variables, but if you have more, you may want to use arrays to manage this.
bounds = [Nx Ny Nz];
values = [X Y Z];
for k = 1:length(bounds)
if values(k) < -bounds(k)
values(k) = bounds(k);
elseif values(k) > bounds(k)
values(k) = -bounds(k);
end
end

Più risposte (1)

Image Analyst
Image Analyst il 16 Giu 2020
Without knowing the values of any of those variables? How are we supposed to do that? Can you attach them in a .mat file
save('answers.mat', 'X', 'Y', 'X', 'Nx', 'Ny', 'Nz');
But I'm sure it's going into, or skipping, whatever if/else block it's supposed to based on the variable values and nesting.
Do you want to do anything in the case that X is in the range [-Nx, Nx]? Currently that case is not handled.
Do you just want to clip values, like
X = min(X, Nx);
X = max(X, -Nx);
instead of all that if stuff?

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by