Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

For loop in matlab

  1. Jan 15, 2012 #1
    hi, i am facing some problem in my for loop or ifesle statement in matlab.
    i used a for loop to load the images from a folder, after the images loaded, it will gone through some calculation to get the average. if the images average <=10, then i want to display the images name in a listbox. let say i have 10 images in my folder, and average for images' 2,4 and 6 is <=10, the images will still go through else statement. if the average of last image is <=10, it just will stop in if statement and wont go through else statement. anyone know what is the problem?

    here is some part of my code:

    sdirectory = 'D:\recognition system\database'; %specific the file directory
    filess = dir([sdirectory '/*.png']);
    for k = 1:length(filess)
    filename = [sdirectory '\' filess(k).name];
    I = imread(filename);


    if( aveg(k)<= 10 )

    lo{k}=sprintf('%% of diffrence for <<%s>> is %G %%\n\n ',filename,aveg(k));


  2. jcsd
  3. Jan 15, 2012 #2


    User Avatar
    Gold Member

    Not sure if you're saying you get an error. If you do, post it.

    If you don't, try removing the ; for aveg(k) value so that it will output the value during run (or just add the line disp(aveg(k)) and confirm you're getting the numbers you expect.
  4. Jan 16, 2012 #3

    thank you for ur reply:)

    there is no error. if i add disp(aveg(k),the output is show in workplace but not my gui listbox. my problem not the display output problem. my problem is if the last images that loaded from folder is not my expected output, then it will continue to else statement even the first few images is my expected output.

    i used if else statement is wan to filter out the expect image and non-expect image.eg:if i loaded 10images, the first 4 images is my expect output, then it should not go through else statement;but now it will run the else statement too.
  5. Jan 16, 2012 #4


    User Avatar
    Gold Member

    I don't comprehend what you're saying, still, I don't know what you're trying to do. And it's hard to troubleshoot your code when you leave gaps in it.

    The reason I had you display that is because it's the variable that your IF line depends on... this is just a step to debug code, not a fix. You would remove the disp after you work out the logic of what your loop is doing.

    you might want to put disp('true') in the if section so that you know which ones go through the if and which ones don't.

    But remember, this is just so that YOU can debug. I still have no idea what you're doing.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook