Borrar filtros
Borrar filtros

Reference a cell array using a string to specify image data for imwrite

2 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 22 de Jun. de 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
Ariel Avshalumov el 22 de Jun. de 2018
Thank you so much for your advice. I don’t really know why I started by creating numbered variables, but this advice will greatly help me in the long run!

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Jun. de 2018
Simply put all of your cell arrays into one structure (or another cell array), and then you can trivially use indexing to access them:
S(1).images = {... cell array one ...}
S(2).images = {... cell array two ...}
...
S(N).images = {... cell array N ...}
then to access them you just need basic indexing:
for k = 1:numel(S)
tmp = S(k).images;
... something with tmp
end
Note that it is better to use fullfile rather than string concatenation:
folder = fullfile(pwd,num2str(i),'Occlude');
  3 comentarios
Stephen23
Stephen23 el 22 de Jun. de 2018
Editada: Stephen23 el 22 de Jun. de 2018
"May I ask why fullfile is better than strcat?"
Simpler (you don't need to call filesep everywhere), deals with edge-cases nicely (e.g. collapses empty inputs, collapses duplicate filesep characters), makes the intention of the code clear, is platform independent (unlike what you wrote), is one function (so has one of the main advantages of functions: it is debugged and improved by TMW in one place), it is written specifically for that task (so its behavior is tailored to dealing with foldernames and paths), it handles inputs with/without file separators (imagine one user gives an input 'C:\mine' but another wrote 'C:\mine\'... how would your code deal with this? fullfile handles this automatically), collapses the single relative directory '.'.
Ariel Avshalumov
Ariel Avshalumov el 22 de Jun. de 2018
Thank you for imparting your coding wisdom. Your knowledge is much appreciated.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by