Inside the loop get the class and determine the proper output folder from it. Call mkdir() to create the folder if it does not exist. Then create original and new full file names with fullfile() and other functions (like maybe fileparts or sprintf). Then use movefile() or copyfile() to move or copy the original file to the new class-specific folder. Write back if you can't figure it out.