Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

'clear all' in FOR loop... pls help to understand the codes

Status
Not open for further replies.

nicleo

Advanced Member level 2
Joined
Sep 6, 2004
Messages
661
Helped
66
Reputation
132
Reaction score
12
Trophy points
1,298
Activity points
7,150
Code:
When the following codes:

% [code 1]
k=5
clear all;
dummy=k

... were executed, Matlab showed an error as follows:
"??? Reference to a cleared variable k."


However, when the following codes:

% [code 2]
for i=1:5
    i
    clear all;
    dummy=i
end

... were executed, Matlab did not show any error. The line dummy=i, when executed, was displayed as follows:

dummy = 0 + 1.0000i
Pls refer to [code 2]. At line 'clear all', variable 'i' was cleared. However, when the program counter looped back to 'i', the value of 'i' was 2. Would anyone pls advise how does Matlab know that it's 2nd iteration or i=2 although variable 'i' was cleared during first iteration?

Thank you very much
 

Answers (obtained from some MATLAB users in other forum) to my OWN question:

[Answer 1]
Sounds perfectly reasonable to me. The loop variable isn't incremented (like it would be in FORTRAN or C). Instead, each loop assigns the next column of the array to the loop variable. In this case, [1 2 3 4 5]. So changing (or clearing) the loop variable in the loop doesn't affect the next value.


[Answer 2]
Thing get even stranger: when you run [code 2] at the command line, it does give an error, if you run it from a m-file it doesn't ... This would mean it has something to do with the compiler/interpreter. A small test shows a possible reason:

% [code 3]
for k=1:5
k
clear all;
dummy=k
end

[code 3] gives an error again. This means the compiler scans the code, sees that the imaginary unit 'i' is used, knows that you cannot clear a value, but half forgets that there is also a variable named i.... Although the variable i was cleared because it doesn't show up in the workspace... Another test to make it complete:

% [code 4]
for I=1:5
I
clear all;
dummy=i
end

Though we know matlab is case sensitive, the imaginary unit i can also be obtained by writing I. [code 4] also doesn't give any error. Maybe this helped you a step further, but I hope someone else can give more insight in the way matlab handles references to its variables internally.


[Answer 3]
if you clear i, i again is the imaginary unit (therefore the dummy = 0 + 1.0000i).
note: you can also change i in the loop and the loop does not get affected, it will continue to update (this is different from e.g. C-Code).
 

Re: 'clear all' in FOR loop... pls help to understand the co

command "clear all" means clear all the variable int the command space.
So after you clear all the variable int the space ,there is no k .There will show an error message for you.
 

Re: 'clear all' in FOR loop... pls help to understand the co

using command 'clear all' means that all the variables before that command are being removed. So after that command, you will receive an error message.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top