1

我真的很难用这个做我想做的事,所以任何帮助都将不胜感激。

我循环遍历数组 X 次,以随机方式显示图像数组。我想要做的是检索当前以随机顺序显示的每个图像的名称,以及它所属的数组,因为每个图像都显示在循环中,并将两者存储在单独的变量中。

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}

因此,例如,如果images.blue在循环遍历数组时显示在屏幕上,我希望将名称blue保存在 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

上面代码发生的情况是,在显示所有图像之后,每次迭代直到满足条件,最后显示的图像的字段名称都存储在CurrentFieldname.

以上不是我想要的。每次在迭代循环中显示图像后,我希望该字段CurrentFieldname包含正在显示的图像的名称。然后,在循环的每次交互过程中,我想比较CurrentFieldNamewithcatacatb,看看CurrentFieldName属于哪个数组。然后,将其记录到一个单独的变量中,CurrCat. 例如

if CurrentFieldname == cata
    CurrCat = a;

我只想拥有这两个变量,CurrentFieldName并且CurrCat在每次迭代结束时都包含相关信息。然后它们都将被与随机显示的下一个图像相对应的信息覆盖,依此类推。

我希望这一切都有意义。

谢谢

4

1 回答 1

0

我们试试看:

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
于 2013-01-03T07:13:09.637 回答