If/else Statement Being Ignored
6 views (last 30 days)
Show older comments
Hello, I'm relatively new to MATLAB and I think I have an if/else statement that appears to be ignored by my code. I am using the statement to define variables for later in my code. When I say "ignored" it appears as though my code only recognizes the second set of variables listed. Meaning, if I set A2 = 10, the code reads A2 = 10 for all t (time), even when t<50. If i set it such that A2 = 10 is in the "if" portion (t<50), it will use A2 = 0 for the entire run. I am going to only include the relevant portions as best I can because the rest of the entire document is simply just a long list of variables and their values that is then called by another script. Any ideas or help would be greatly appreciated.
global k Xind B C D E F G I K W Z Y A1 A2 X0
tTreat = 50;
if (t < tTreat)
B = 0;
C = 0;
D = 0;
E = 0;
F = 0;
G = 0;
I = 0;
K = 0;
W = 0;
Y = 0;
Z = 0;
A1 = 0;
A2 = 0;
else
B = 0;
C = 0;
D = 0;
E = 0;
F = 0;
G = 0;
I = 0;
K = 0;
W = 0;
Y = 0;
Z = 0;
A1 = 0;
A2 = 10;
end
X0 = zeros(404,1);
k = zeros(148,1);
Xind = zeros (138,1);
tspan = 0:0.01:100;
X0(135) = 1.41e3; %(etc.)
3 Comments
Image Analyst
on 20 Feb 2017
Adam's answer is very good. Sooner or later (hopefully sooner) you'll need to learn to use the debugger (like the rest of us) to step through your code to examine variables and see what lines of code get executed. See this tutorial http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Guillaume
on 20 Feb 2017
While totally unrelated to the problem (which has been answered), the use of so many global variable is a likely source for future bugs. The further lack of imagination in the naming of these globals will also in great likelyhood lead to bugs, and certainly won't help with maintaining the code.
Answers (2)
Image Analyst
on 20 Feb 2017
Setting B=10 has no effect on the test in the "if" statement. The if statement only looks at whether t is less than tTreat, and doesn't care about whether B is 10 or any other number. Why do you think B should have any effect on whether the "if" or the "else" block is executed?
Walter Roberson
on 20 Feb 2017
Your "t" is a vector or array, and not all of the elements are < 50 . or t is empty.
If you have vector t then t<tTreat calculates a vector of logical results. Then the if statement only considers the condition to be true if all of the results are non-zero, which fails if the result is empty and fails if some of the results were true but others were not.
If you are working with vectors or arrays then you should be using loops to handle the situation one at a time, or you should be switching to using logical indexing to construct vectors of results. For example,
zt = zeros( size(t) );
B = zt;
C = zt;
D = zt;
E = zt;
F = zt;
G = zt;
I = zt;
K = zt;
W = zt;
Y = zt;
Z = zt;
A1 = zt;
A2 = zt;
mask = t >= tTreat;
A2(mask) = 10;
Here all of the variables would have values the same size as t so that you can select the value on a per-member-of-t basis. Then you use logical indexing to set the other value only in the portions of the variable that need the other value.
2 Comments
Guillaume
on 20 Feb 2017
Well, clearly if you get this error one or both of t or tTreat is not scalar.
'My "t" should not be a vector or array I believe'
There should not be any should or I believe. Use the debugger as you've been told (just double click in the grey margin to put a breakpoint on the if line) and see what it actually is.
See Also
Categories
Find more on Loops and Conditional Statements 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!