Solving MATLAB For/If-Else loop Problem

  • MATLAB
  • Thread starter jaclyn89
  • Start date
  • Tags
    Loop Matlab
In summary, if the last image in the folder is not the expected average, it will continue to the else statement even if the first few images are the expected average.
  • #1
jaclyn89
5
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5



There could be a few potential issues with your code that could be causing the problem you are facing. Here are a few things to consider:

1. Make sure that the calculation for average is correct and giving the expected result. You can try printing out the value of aveg(k) at each iteration to see if it matches your expectations.

2. Check the condition in your if statement. It is possible that the condition is not being met for the last image, which is why it is not going through the if statement. Double check the values of aveg(k) and see if they are indeed less than or equal to 10.

3. Instead of using a for loop, you can also try using a while loop and a counter variable to keep track of the number of images with an average less than or equal to 10. This way, you can display the names of all the images in the listbox at once.

4. Another potential issue could be with the way you are updating the listbox. Make sure that the listbox is being updated at each iteration of the loop and not just for the last image.

Overall, it is important to carefully check your code and make sure that all the variables and conditions are correctly set up. You can also try debugging your code by printing out values and checking for errors. Additionally, you can refer to the MATLAB documentation or seek help from fellow MATLAB users to troubleshoot any specific issues you may be facing.
 

FAQ: Solving MATLAB For/If-Else loop Problem

What is a For/If-Else loop in MATLAB?

A For/If-Else loop in MATLAB is a programming construct that allows you to execute a block of code repeatedly while a condition is met. The "for" loop is used to iterate over a specific range of values, while the "if-else" statement is used to make decisions based on certain conditions.

How do I write a For/If-Else loop in MATLAB?

To write a For/If-Else loop in MATLAB, you first need to define the range of values that you want to iterate over using the "for" loop. Then, you can use the "if-else" statement to check for specific conditions and execute different blocks of code accordingly.

What are the benefits of using For/If-Else loops in MATLAB?

For/If-Else loops in MATLAB can help you save time and effort by automating repetitive tasks. They also allow you to make decisions based on different conditions, making your code more flexible and efficient.

Can I use For/If-Else loops for any type of problem in MATLAB?

For/If-Else loops can be used for a wide range of problems in MATLAB, such as data manipulation, image processing, and mathematical computations. However, there may be certain cases where other programming constructs may be more suitable.

How can I debug my For/If-Else loop in MATLAB if it is not working correctly?

If your For/If-Else loop is not working correctly, you can use the debugging tools in MATLAB to identify and fix any errors. These tools allow you to step through your code and check the values of variables at different stages, helping you pinpoint the source of the issue.

Similar threads

Replies
3
Views
2K
Replies
6
Views
5K
Replies
3
Views
2K
Replies
2
Views
3K
Replies
7
Views
3K
Back
Top