Vertcat error when concatenating images in loop

1 view (last 30 days)
I am reading around 1000 images showing progress over time. I want to concatenate them to show daily average.
However, the vertcat shows error "Dimensions of arrays being concatenated are not consistent." after 126 images.
All images are same size (1680 x 965) so I'm not really sure why the erros occurs after 126 images.
Anyone an idea why and how to resolve it?
Thanks in advance!
Img = dir([path,strcat('**/S_1_*')]);
t = [];
Sav = [];
for i = 1:length(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sro=imread(Img(i).name);
Sav = [Sav; mean(vertcat(Sro),1,'omitnan')];
end

Answers (1)

Jan
Jan on 29 Sep 2022
Edited: Jan on 29 Sep 2022
vertcat(Sro) concatenates Sro with nothing. What is the purpose of this command?
Ask Matlab, what the problem is:
for i = 1:length(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sro = imread(Img(i).name);
try
Sav = [Sav; mean(Sro, 1, 'omitnan')];
catch ME
size(Sav)
size(mean(Sro, 1, 'omitnan'))
error(ME.message)
end
end
By the way, a pre-allocation is more efficient thanletting an array grow iteratively:
Sav = zeros(numel(Img), 965);
for i = 1:numel(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sro = imread(Img(i).name);
try
Sav(i, :) = mean(Sro, 1, 'omitnan');
catch ME
size(Sav)
size(mean(Sro, 1, 'omitnan'))
error(ME.message)
end
end
  2 Comments
Siegmund Nuyts
Siegmund Nuyts on 30 Sep 2022
Thanks for the suggestion.
The error gives: "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
The pictures show change over time so concatenating them will show "average" change.
The x-axis will then be "time" and y-axis distance.
The outcome is exactly what I need. I just don't understand why/how it gives an error after a certain amount of pictures...?
Jan
Jan on 30 Sep 2022
@Siegmund Nuyts: Please post the complete output of the error message: In which line does Matlab stop? Which sizes are displayed? Did you understand, why the sizes of the arrays are displayed before the error message? This information will show you, why the sizes are not matching. Maybe some of the images are RGB images (3D) and others in gray scale (2D)?

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!