Loop, function or memory problem: Loop variable in a function is limited automatically.

Hi to all,
I am processing 3D images. In one of the functions i wrote, "for" loop variable is limited to 262144 while it should not be. My loop is just like :
For i = 1 : 26214400 %( 1 : 512*512*100 ) .... end
In case it is a memory problem, i deleted some data to get more space on the memory my script uses but it did not work. The limit value does not change. I tried it on both uint8 and double data.
By the way i use Windows 7, i have 4 GB Memory. I strongly need help about this.
Thanks to all.
Ibrahim

7 Comments

Could you cut and paste the actual code you use? Clearly, the example in your question is not the code, because it has "For" instead of "for".
check the size of your image (is it really 512x512x100 or is it just 512x512)?
Thanks Robert, David and Gautam.
@Robert Cumming :
There is no error message. It just stops and quit when i = 262144.
@David Young : Well, i did not write "For" on .m file. It is "for".
@Gautam Vallabha : In case of any mistake like that, i wrote
" for i = 1 : 26214400 " not for i = 1 : 512*512*100. And even i tried other numbers like "for i = 1 : 2000555, which is greater than 262144.
Hmm, something weird is going on. Can you verify that this works at the command line:
for ii = 1:512*512*100,jj = ii/2;end,jj*2==512*512*100
So it works at the command line, it should work in the function. There is more to the story that we cannot see because we don't have the code....

Sign in to comment.

 Accepted Answer

First of all, thank to you all. Well, i figured it out. I was usually setting end value of i as some value different than 26214400. So when Matt said about 512*512 which is 262144 i checked again some sub-functions called in my function. And i realized that my array limited by 512*512 by one of the sub-functions.
But the weird thing is that i would expect to get error when i try to reach (262144 + 1)th element.
Actually, i use the array to hold some indices and i check if there is any new indices added or not by using the next element of the array at the end of "for" loop :
%array was set by sub-functions as % array = zeros(262144) %and if there is no change in "i"th loop, array(i+1) is zero.
ind = 1; for i = 1 : n
if expression_a
array(ind) = exp1;
ind = ind + 1;
end
if expression_b
array(ind) = exp2;
ind = ind + 1;
end
if (array(i+1) = 0), break, end
end
Wouldn't that be the error like "Index exceeds matrix dimensions" ? Or do i miss something?
Thanks again. Is there anyone currently working on medical image processing?

6 Comments

And i owe you an apology. I took your time for just a mistake i made.
No apology needed, it happens all the time to everyone. I was hoping that by demonstrating that FOR loops weren't clipped at the command line it would spur you to investigate further. Looks like it worked!
That code would fail because "=" cannot be used in the middle of an "if" statement.
Well, i wrote it wrong here. Of course it would. But it is == in my .m file. Or it would never work because of syntax error.
@Matt: Yes Matt, it happens sometimes. Thank you.

Sign in to comment.

More Answers (2)

What exactly does "stops and quit" mean? Did you use the debugger to find out, which line causes the break? Just insert a "if i == 262144, disp('catch'); end" and set a break point on the disp line. Then step through the code line by line. It is impossible for us to guess, which command in which line is responsible.
Hmm, something weird is going on. Can you verify that this works at the command line:
for ii = 1:512*512*100,jj = ii/2;end,jj*2==512*512*100
Also, in your loop is there a BREAK or RETURN statement anywhere?

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!