How do I create a loop for a .m file that has multiple functions in it?

3 views (last 30 days)
I have modified a .m file from https://www.mathworks.com/matlabcentral/fileexchange/25682-color-threshold (It takes an image and allows user to make some modifications to it).
Basically it is an .m file with multiple functions in it. I want to be able to run through multiple pictures contained in a specified folder. From my understanding I can't run a loop for all the functions, so I created a script as the following:
waitfor(msgbox('Choose the directory your pictures are in'))
directory=uigetdir; %Saves all files to this location
cd(directory)
i=1;
listFiles = dir('*.jpg');
for i=1:length(listFiles)
ColorThreshold(listFiles, i, directory)
end
However, it only loops through the first function of "ColorThreshold" (Cropping the image). It isn't until the last image that it actually runs through all the functions? I would like for it to go through all the functions contained in the .m file for every single picture. I am confused as why it only goes through all the functions in the .m file for the last image.
**Edit, it seems that it is running through all the functions in the .m file, but it is not waiting for any user input with the other functions. Is there a way for me to edit the functions so the loop only continues when I press a certain button?
Thanks for any help.

Accepted Answer

Guillaume
Guillaume on 19 Dec 2016
Note: local functions (functions below the main function in the main file) can only be called by the main function and are invisible to functions / scripts in other files. That's fine and is not your problem.
"It isn't until the last image that it actually runs through all the functions" No, your script never calls any of the local functions (because it can't) and it doesn't need to. There is actually nothing wrong with your script (except that it doesn't index listFiles in the loop), the problem is with the way the function is coded, it can't be called in a loop, because:
  1. each time it is called, it closes all existing figures, even the one it created in a previous call,
  2. even if it didn't close existing figure, the figure number it uses is hard coded, so it would reuse the same figures,
  3. the function creates figures, sets callback on these figures and then returns. The callbacks are then called by matlab when the user interacts with the figures. So, most of the code is actually executed asynchronously, depending on when the user interacts on the figure. That's not appropriate for a loop.
Probably, the best way to work around this is to stop the function from returning until the user has finished interacting with the figures, using uiwait in the main function, and uiresume in a Close button callback.
  1 Comment
Landon Ingle
Landon Ingle on 19 Dec 2016
Perfect. Thank you for your in depth answer. I got it working- but more importantly understand why it wasn't working.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations 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!