MATLAB Array Iteration&字段名

时间:2013-01-03 05:13:03

标签: matlab random iteration cell

我真的很难做到我想做的事情,所以任何帮助都会受到高度赞赏。

我循环遍历数组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保存在变量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包含正在显示的图像的名称。然后,在通过循环的每次交互期间,我想将CurrentFieldNamecatacatb进行比较,以查看哪个数组CurrentFieldName属于。然后,将其记录到单独的变量CurrCat中。 E.g。

if CurrentFieldname == cata
    CurrCat = a;

我只想让这两个变量CurrentFieldNameCurrCat在每次迭代结束时都包含相关信息。然后它们都会被与随机显示的下一个图像相对应的信息覆盖,等等。

我希望这一切都有道理。

由于

1 个答案:

答案 0 :(得分: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