Creating an array with variables or a loop

1 view (last 30 days)
I have the next code:
for k=1:10
imDs{k}=imread([num2str(k),'.png']);
iGrisB{k} = imDs{k}(:, :, 3);
%Datos
media{k} = mean(iGrisB{k}(:));
Std{k}=std(double(iGrisB{k}(:)));
DMA{k}=mad(double(iGrisB{k}(:)));
MIN{k}=min(iGrisB{k}(:));
MAX{k}=max(iGrisB{k}(:));
Mode{k}=mode(double(iGrisB{k}(:)));
end
dat={media{1},Std{1},DMA{1},MIN{1},MAX{1},Mode{1}
media{2},Std{2},DMA{2},MIN{2},MAX{2},Mode{2}
media{3},Std{3},DMA{3},MIN{3},MAX{3},Mode{3}
media{4},Std{4},DMA{4},MIN{4},MAX{4},Mode{4}
media{5},Std{5},DMA{5},MIN{5},MAX{5},Mode{5}
media{6},Std{6},DMA{6},MIN{6},MAX{6},Mode{6}
media{7},Std{7},DMA{7},MIN{7},MAX{7},Mode{7}
media{8},Std{8},DMA{8},MIN{8},MAX{8},Mode{8}
media{9},Std{9},DMA{9},MIN{9},MAX{9},Mode{9}
media{10},Std{10},DMA{10},MIN{10},MAX{10},Mode{10}};
t=uitable;
ColNames={'Media','DS', 'DMA','Min','Max','Mode'};
set(t,'Data',dat,'ColumnName',ColNames, ...
'Position', [40 225 525 400])
My problem is that I want to find an easier way to do the part of dat={media{1},Std{1}...media{10},Std{10}} because they are not only 10 images, I need to process almost 60 images. Also it has to be in a flexible way, because not always I have to process 60 images sometimes less or more. Thank you. Regards.

Accepted Answer

Jan
Jan on 26 Jun 2014
Edited: Jan on 26 Jun 2014
You can create the output directly:
n = 10;
dat = cell(n, 6);
for k = 1:n
imDs = imread([num2str(k),'.png']);
iGrisB = imDs(:, :, 3);
B = double(iGrisB(:));
dat(k, :) = {mean(B), std(B), mad(B), min(B), max(B), mode(B)};
end
Another option is the original loop an this for the joining:
dat = cat(1, media, Std, DMA, MIN, MAX, Mode).';

More Answers (1)

Ben11
Ben11 on 26 Jun 2014
You can move the dat(...) inside your for-loop:
dat =cell(NumFiles,6); % NumFiles is the # of images you have.
for k=1:NumFiles
imDs{k}=imread([num2str(k),'.png']);
iGrisB{k} = imDs{k}(:, :, 3);
%Datos
media{k} = mean(iGrisB{k}(:));
Std{k}=std(double(iGrisB{k}(:)));
DMA{k}=mad(double(iGrisB{k}(:)));
MIN{k}=min(iGrisB{k}(:));
MAX{k}=max(iGrisB{k}(:));
Mode{k}=mode(double(iGrisB{k}(:)));
dat(k,:) = {media{k} Std{k} DMA{k} MIN{k} MAX{k} Mode{k}};
end
t=uitable;
ColNames={'Media','DS', 'DMA','Min','Max','Mode'};
set(t,'Data',dat,'ColumnName',ColNames, ...
'Position', [40 225 525 400])

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!