How to extract variables from a structure?

2 Ansichten (letzte 30 Tage)
heo jaemu
heo jaemu am 6 Feb. 2015
Beantwortet: Geoff Hayes am 7 Feb. 2015
I have a structure like above image.
the structure is that load all jpg files in folder.
(so many files i have, so i want to read automatically.)
but, I can't use structure.
I want to variables.
I try this.
a = dir('C:~~~~~\*.jpg');
for i = 1 : length(a)
filename = strcat('C:~~~~\',a(i).name);
I = imread(filename);
end
finally, what I want is same result of imread.
image(i) = ~~~~ % = imread('~~~.jpg'
image(2) = ~~~~
...
image(n) = ~~~~

Antworten (1)

Geoff Hayes
Geoff Hayes am 7 Feb. 2015
Heo - try the following. Create a cell array outside of your loop to store the data that you read on each iteration of the loop. Something like
myFolder = pwd;
myFiles = dir([myFolder '/*.jpg']);
myImages = cell(length(myFiles),1);
for k=1:length(myFiles)
filename = fullfile(myFolder,myFiles(k).name);
myImages{k} = imread(filename);
end
The above uses pwd to get the current directory and looks for all jpg files within it. We then pre-size the myImages cell array to populate with each read image. fullfile is used to correctly build the path to the file of interest. Try the above and see what happens!

Kategorien

Mehr zu Read, Write, and Modify Image finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by