# Vertcat error when concatenating images in loop

2 visualizzazioni (ultimi 30 giorni)
Siegmund Nuyts il 29 Set 2022
Commentato: Jan il 30 Set 2022
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?
Img = dir([path,strcat('**/S_1_*')]);
t = [];
Sav = [];
for i = 1:length(Img)
t = [t; datenum(Img(i).name(5:end-4),'yyyymmddHHMM')];
Sav = [Sav; mean(vertcat(Sro),1,'omitnan')];
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

Jan il 29 Set 2022
Modificato: Jan il 29 Set 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')];
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')];
try
Sav(i, :) = mean(Sro, 1, 'omitnan');
catch ME
size(Sav)
size(mean(Sro, 1, 'omitnan'))
error(ME.message)
end
end
##### 2 CommentiMostra NessunoNascondi Nessuno
Siegmund Nuyts il 30 Set 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 il 30 Set 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)?

Accedi per commentare.

### Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by