Loading images with their names into single arrays

3 visualizaciones (últimos 30 días)
Hi, I'm trying to load multiple .tif files into MATLAB from a folder. I tried this:
files= dir('*.tif')
for k=1:length(files);
imagename = files(k).name;
image1 = imread(imagename);
images{k} = image1;
end
But I need these files to be loaded separately, so every one of them will be one single array, but this code writes them all into a cell. I also need these single array variables to have the same names that .tif files had. How can I do that?
  5 comentarios
Stephen23
Stephen23 el 4 de Oct. de 2020
Editada: Stephen23 el 4 de Oct. de 2020
"But I need these files to be loaded separately, so every one of them will be one single array..."
They already are: every cell of images contains one single array. They can be trivially accessed using indexing.
":..but this code writes them all into a cell."
Which is one of the best and simplest ways to store data from multiple files, and is exactly what the MATLAB documentation recommends: https://www.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html
"I also need these single array variables to have the same names that .tif files had."
That would be about the worst way to write MATLAB code. Forcing meta-data into variable names is one way that some beginners force themselves into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
Consider what would happen if you tried to name variables after filenames, and some of the files had these names: '-1.tif', 'a-b.tif', '0.tif', '@.tif'
Izabela Wojciechowska
Izabela Wojciechowska el 4 de Oct. de 2020
Thank you for help @Sindar and for the explanation @Stephen Cobeldick.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 4 de Oct. de 2020
One of the best approaches is to simply import the file data into the same structure that dir returns, then for each file you have all of the file data and file meta-data in one simple structure element:
D = 'absolute or relative path to where the files are saved';
S = dir(fullfile(D,'*.tif'))
for k = 1:numel(S);
F = fullfile(D,S(k).name);
S(k).data = imread(F);
end
You can trivially access any file using indexing, e.g. the third file:
S(3).name
S(3).data
See also:
  3 comentarios
Stephen23
Stephen23 el 4 de Oct. de 2020
@Izabela Wojciechowska : there are two ways to use data stored n container arrays:
  1. assign the content to a temporary variable
  2. refer to the content of the container array directly
The first is very intuitive and makes it easy to use your existing code:
for k = 1:numel(S)
Regions = S(k).data;
x = find(Regions==5);
... your code here
end
The second you simply replace every instance of Regions with S(k).data, e.g.:
for k = 1:numel(S)
x = find(S(k).data==5);
... your code here
end
Izabela Wojciechowska
Izabela Wojciechowska el 4 de Oct. de 2020
Thank you, it works. I really appreciate your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by