Reputation: 245
I'm really struggling to do what I want with this so any help will be greatly appreciated.
I'm looping through an array X number of times, displaying an array of images in a randomised manner. What I want to do is retrieve the name of each image that is being currently displayed in the random order, and the array it belongs to, as each image is displayed in the loop, and store the two in separate variables.
images.blue = imread('blue.bmp');
images.red = imread('red.bmp');
images.green = imread('green.bmp');
images.yellow = imread('yellow.bmp');
colours = {images.blue, images.red, images.green, images.yellow}
cata = {images.blue, images.red}
catb = {images.green, images.yellow}
So for example if images.blue
is being displayed on the screen while the loop is iterating through the array, I want the name blue
to be saved in variable CurrentFieldname
.
for count = 1;
names = (fieldnames(images));
while count <= 5
for n = randperm(length(colours));
d =(colours{n});
imshow(d);
pause(1)
CurrentFieldName = (names {n});
end
count = count+1;
end
break
end
What happens with the above code is that after all the images have been displayed, every iteration up till the condition is satisfied, the field name of the last displayed image is stored in CurrentFieldname
.
The above is not what I want. After every time an image is displayed in the iterating loop, I want the field CurrentFieldname
, to contain the name of the image that is being displayed. Then, during each interation through the loop, I would like to compare the CurrentFieldName
with cata
and catb
, to see which array CurrentFieldName
belongs to. Then, record this into a separate variable, CurrCat
. E.g.
if CurrentFieldname == cata
CurrCat = a;
I would just like to have both of these variables, CurrentFieldName
and CurrCat
, contain the relevant information at the end of every iteration. Then they would both be overwritten by the information corresponding to the next image that is randomly displayed and so forth.
I hope this all makes sense.
Thanks
Upvotes: 1
Views: 297
Reputation: 114796
Lets try:
colors = {'red', 'green', 'blue', 'yellow' }; % existing colors, assuming for each color there is a .bmp image
NC = numel( colors ); % number of colors
% read the images once
for ci=1:NC
img.(colors{ci}) = imread( [colors{ci}, '.bmp' ] );
end
NumIters = 1; % number of iterations over all colors
% reapeat as many times
for itr = 1:NumIters
ord = randperm( NC ); % permutation for this iteration
for ci = ord(:)', %'
CurrentFieldName = colors{ci};
imshow( img.(colors{ci}) ); % display the current color image
title( colors{ci} );
tic;
% your code here processing img.(colors{ci})
Response = myFun( img.(colors{ci}) );
ResponseTime = toc; % record timing of processing
save( sprintf('data_for_itr%03d_color%02d.mat', itr, ci ),...
'CurrentFieldName', 'Response', 'ResponseTime' );
end
end
Upvotes: 0