Reference a cell array using a string to specify image data for imwrite
Show older comments
I have a group of cell arrays. Each one contains a number of cells which have images.
Ex: >> images
images1 =
1×5 cell array
Columns 1 through 2
{256×256 uint8} {256×256 uint8}
Columns 3 through 4
{256×256 uint8} {256×256 uint8}
Column 5
{256×256 uint8}
The cell arrays are listed like this: Images1 Images2 Images3 etc
What I want to do is create a loop which iterates through these cell arrays and writes the images to separate folders.
Here is what I have so far:
for i = 1:4 %this is how I reach the target folder I want to write images to
folnum=num2str(i);
folder = strcat(pwd, '\', folnum, 'Occlude', '\'); %the path
for image = 1:5 %example number
num = num2str(image);
name=strcat(folder, num, png);
*** cell_array = strcat('Images', image);
*** imwrite(cell_array{image}, name);
end
end
I know the problem lies in the two lines I * above. I am creating a character vector called cell_array when I want to reference a variable. I have seen before that it is possible to remedy this by using structures but I am not sure how to go about doing that. Any advice or suggestions would be much appreciated.
2 Comments
Stephen23
on 22 Jun 2018
"The cell arrays are listed like this: Images1 Images2 Images3 etc"
And that is the cause of your difficulties. If you are numbering variables then you are doing something wrong. Trying to access numbered variables is one way that beginners force themselves into writing slow, complex, buggy code. Read this to know more:
Putting numbers into variable names like that means that you are using them as pseudo- indices, because they define a unique reference and order, just like real indices do. But unlike real indices they are hard to work with and make your code pointlessly complex and slow. So you would be much better off converting those pseudo indices into real indices, and then you can write simpler, faster, neater, more efficient, less buggy code.
Ariel Avshalumov
on 22 Jun 2018
Accepted Answer
More Answers (0)
Categories
Find more on Convert Image Type 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!