- you assign something else to it. The clearing occur at the point where the assignment occurs and only then.
- it goes out of scope (e.g. a variable created inside a function is cleared when the function ends)
- you explicitly clear it
While assigning a variable, is the variable cleared by default before the assignment?
5 views (last 30 days)
please clarify my doubt regarding variable assignment in MATLAB.
In line 2, we can see that output is written with value from someCalculation(). Is output, internally cleared before executing the next loop?
The background of my question is, one of our scripts (I realise scripts are bad. But legacy scripts), was creating a variable 'output' - (quite a huge variable - 1000s of MBytes) in every iteration.
In one of the iteration, there was memory error in Line 2. I have inserted a 'clear output' between line 1 and 2, and expect script to run without memory error.
Is my expectation correct?
In other words, while creating output for ith iteration, is the size of output from i-1 th iteration a bottleneck in terms of memory?
Guillaume on 15 May 2019
Edited: Guillaume on 15 May 2019
Is output, internally cleared before executing the next loop?
No. A variable is only cleared if
Anything else would make no sense and break a lot of code.
I have inserted a 'clear output' between line 1 and 2, and expect script to run without memory error
If someCalculation does create a GB sized output, then yes it will make some difference. The previous content of output gets cleared when you assign the new result of someCalculation and only then. So, while someCalculation is running, with its own internal variable that takes GB of memory, you also have the previous content of output sill in memory. Rather than clear I'd recommend setting output to .
while creating output for ith iteration, is the size of output from i-1 th iteration a bottleneck in terms of memory?
Only when someCalculation is being executed.
Of course, all this assumes that you haven't made any copy of output. Otherwise, these copies still take some memory.