Solving MATLAB For/If-Else loop Problem

  • Context: MATLAB 
  • Thread starter Thread starter jaclyn89
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB programming issue related to the use of for loops and if-else statements. Participants are exploring the logic of a script designed to load images from a folder, calculate their averages, and display certain image names based on those averages in a GUI listbox.

Discussion Character

  • Technical explanation
  • Debugging
  • Conceptual clarification

Main Points Raised

  • One participant describes a problem where, despite some images meeting a certain condition (average <= 10), the code still executes the else statement for the last image, which is not the expected behavior.
  • Another participant suggests checking for errors and recommends displaying the average values during execution to confirm they are as expected.
  • A participant expresses confusion about the original poster's explanation and emphasizes the need for clarity in the code to facilitate troubleshooting.
  • There is a suggestion to add debug statements to track which conditions are being met within the if-else structure.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the underlying issue or its resolution. There is a shared understanding that debugging is necessary, but differing interpretations of the problem persist.

Contextual Notes

The discussion highlights potential gaps in the provided code snippets and the need for clearer explanations of the intended logic and flow of the program.

jaclyn89
Messages
5
Reaction score
0
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 won't 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);

...
aveg(k)=abs((standev/mean1)*100);

if( aveg(k)<= 10 )


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

else

set(handles.listbox1,'String','');
end
end
 
Physics news on Phys.org
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.
 
Pythagorean said:
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.


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.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K