MATLAB giving 0 value for if/then statement

  • Context: MATLAB 
  • Thread starter Thread starter bakin
  • Start date Start date
  • Tags Tags
    Matlab Value
Click For Summary
SUMMARY

The forum discussion addresses an issue in MATLAB where the variable Z returns a value of zero due to the improper scope of the loop variable i. The user initially attempts to use an if/then statement to modify the values of Z based on the conditions of D and a. However, the value of i is not defined outside the for loop, leading to unexpected results. The solution provided involves embedding the if/then statement within a second for loop to ensure that i is correctly defined for each iteration.

PREREQUISITES
  • Understanding of MATLAB syntax and control structures
  • Familiarity with for loops in MATLAB
  • Basic knowledge of conditional statements in programming
  • Experience with variable scope and lifetime in MATLAB
NEXT STEPS
  • Learn about MATLAB variable scope and how it affects loop execution
  • Explore MATLAB control flow statements, specifically nested loops
  • Investigate debugging techniques in MATLAB to identify variable issues
  • Study MATLAB array indexing and its implications in conditional logic
USEFUL FOR

MATLAB users, software developers, and engineers who are working with conditional logic and loops in their code, particularly those troubleshooting similar issues in their scripts.

bakin
Messages
55
Reaction score
0
Hi again! Working with an if/then statement and running into trouble. Here's my code.

Code:
a=2;
N = input('Enter the number of applied loads:');
for i=1:N
    L(i)=input('Enter the force of the applied load in kN:');
    D(i)=input('Enter the position of the applied load in meters:');
end
if D(i) > a
    Z(i)=L(i)*-1
else Z(i)=L(i)
end
And here is the result:
Code:
Enter the number of applied loads:2
Enter the force of the applied load in kN:1
Enter the position of the applied load in meters:1
Enter the force of the applied load in kN:3
Enter the position of the applied load in meters:3

Z =

     0    -3

>> D

D =

     1     3

>> L

L =

     1     3

Why is Z(1) coming out zero? With my if/then statement, what I want is if D>a, then L= -1*L. If not, it should just stay the same, not return a zero.

Any ideas? :confused:edit: Interesting...
Code:
>> projectdebug
Enter the number of applied loads:10
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5
Enter the force of the applied load in kN:5
Enter the position of the applied load in meters:5

Z =

     0     0     0     0     0     0     0     0     0    -5

What's going on?! :cry:
 
Last edited:
Physics news on Phys.org
I think you want to embed this code in a for loop as well:
Code:
if D(i) > a
    Z(i)=L(i)*-1
else Z(i)=L(i)
end

After your first loop exits, what's the value in i? I don't know, and your problem is probably related to i not being defined outside your loop. Alternatively, i could be the first value that caused it to exit the first for loop, which is probably not what you intended.
 
That's it! :smile: After the loop, it put i=2, which probably caused problems. I just threw the same if/then statement in another for loop, with "for i=1:N", and it works. Thanks a bunch, Mark! :wink:
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
3
Views
2K