MATLAB Answers

How do I write images into folders specified in string array?

2 views (last 30 days)
Eric Martz
Eric Martz on 3 Sep 2019
Commented: Image Analyst on 9 Sep 2019
Hi,
I have a 3545 x 2 string array. I attached the first 10 rows as an example:
namesandlabels(1:10, :)
ans =
10×2 string array
"Image100.241.jpg" "Membrane-Stained Cells"
"Image100.242.jpg" "Membrane-Stained Cells"
"Image100.251.jpg" "Membrane-Stained Cells"
"Image100.252.jpg" "No Cells"
"Image100.261.jpg" "Membrane-Stained Cells"
"Image100.262.jpg" "Membrane-Stained Cells"
"Image100.271.jpg" "No Cells"
"Image100.272.jpg" "No Cells"
"Image100.281.jpg" "No Cells"
"Image100.282.jpg" "Intracellular-Stained Cells"
As you can see, there are three different categories of the images. I want to write these images into one of three folders, according to which category they belong to. For example, I want to write the first image into a folder called "Membrane-Stained Cells" and so on for all 3545 images.
How do I do this?
Thanks in advance.

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 3 Sep 2019
Try something like
for k = 1 : size(namesandlabels, 1)
outputFolder = namesandlabels(k, 2);
thisBaseFileName = namesandlabels(k, 1);
sourceFullFileName = fullfile(pwd, thisBaseFileName); % Assume the image is in this folder.
destinationFullFileName = fullfile(outputFolder, thisBaseFileName); % In some other folder.
copyfile(sourceFullFileName, sourceFullFileName);
end

  2 Comments

Eric Martz
Eric Martz on 8 Sep 2019
Thanks for the answer and sorry for not responding earlier. This code doesn't work because it just says that it cannot copy a file onto itself.
Image Analyst
Image Analyst on 9 Sep 2019
Make sure your output folder is not the current folder. I bet that is what is happening. Make sure you have the right input folder and right output folder when you call fullfile().

Sign in to comment.

Sign in to answer this question.