Why does the following function produce error (Index in position 2 exceeds array bounds)?

Why does the following function produce error-
Index in position 2 exceeds array bounds.
Error in POD_DMD (line 47)
imagesc(reshape(Phi(1:r*c,mode),r,c));axis
image;set(gca,'Ydir','Normal')
close all
figure('name','POD')
subplot(figs,1,1)
imagesc(reshape(Phi(1:r*c,mode),r,c));axis image;set(gca,'Ydir','Normal')
title(['\Phi',sprintf('_%i (u-component)',mode)])
subplot(figs,1,2-d)
if com == 2
imagesc(reshape(Phi(r*c+1:end,mode),r,c));axis image;set(gca,'Ydir','Normal')
title(['\Phi',sprintf('_%i (v-component)',mode)]);
end
Please suggest possible solution.
Thanks!

10 Comments

I'm going to guess that size(Phi,2) is less than mode. Without knowing what any of these values are or what size anything is, all anyone can do is guess as to what the root cause is. It's not in the code that's shown.
Thanks for your responce,
This is the complete code.
clear all;
clc;
%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%%%%
freq = 5; %Hz; aquisition frequency
example = '4_68_image'; % input case
mode=2; % which mode to plot and extract DMD from
%%%%%%%%%%%%%%%%%%% SCRIPT %%%%%%%%%%%%%%%%%%%%%
% Import the images from folder. The images are extracted from the Schlieren
% video for wedge_4_68 at Mach 2.5.
data = [];
% if strcmp(example,'image')
directory = '4_68_image';
count = 1;
while exist(sprintf('%s/scene_%0.4d.bmp',directory,count)) ~= 0
data(:,:,count) = imread(sprintf('%s/scene_%0.4d.bmp',directory,count));
count = count +1;
end
r = size(data,1); c = size(data,2);
% Transform the data into column vectors.
data=reshape(data,r*c,size(data,3));
% end
% if strcmp(example,'groynes')
% directory = '4_68_image';
% count = 1;
% while exist(sprintf('%s/scene_%0.4d.bmp',directory,count)) ~= 0
% load(sprintf('%s/scene_%0.4d.bmp',directory,count))
% data_u(:,:,count) = u;
% data_v(:,:,count) = v;
% count = count +1;
% end
% r = size(data_u,1); c = size(data_u,2);
% % Transform the data into column vectors.
% data=[reshape(data_u,r*c,size(data_u,3));reshape(data_v,r*c,size(data_v,3))];
%%%%%%%%%%%%%%%%%%% POD %%%%%%%%%%%%%%%%%%%%%%
% Perform the POD - this is mean subtracted for POD not for DMD
[Phi ,~, C]=svd(data-repmat(mean(data,2),[1 size(data,2)]),'econ');
% Plot the figures
figs = 3;com=1;d=1;
close all
figure('name','POD')
subplot(figs,1,1)
imagesc(reshape(Phi(1:r*c,mode),r,c));axis image;set(gca,'Ydir','Normal')
title(['\Phi',sprintf('_%i (u-component)',mode)])
subplot(figs,1,2-d)
if com == 2
imagesc(reshape(Phi(r*c+1:end,mode),r,c));axis image;set(gca,'Ydir','Normal')
title(['\Phi',sprintf('_%i (v-component)',mode)]);
end
subplot(figs,1,3-d)
plot(C(:,mode))
title(sprintf('C_%i',mode))
subplot(figs,1,4-d)
[px, fx]=pwelch(C(:,mode),round(0.9*size(data,2)),round(0.8*size(data,2)),...
2^12,freq);
plot(fx,px);
title(sprintf('P(C_%i)',mode))
set(figure(1),'position',[246 66 370 732])
waitfor(msgbox('Please select the peak of the POD coefficient spectra (bottom image) you would like to extract'))
[x, y]=ginput(1);
[locs, vals]=findpeaks(px);
[v, l]=min(abs(fx(vals)-x));
hold on
plot(fx(vals(l)),locs(l),'ro')
You read in either no files or else exactly one file. What shows up for count ?
I am reading multiple image frames, and images goes from scene_0001.bmp - scene_0062.bmp. Count is for images for 1 to 62.
I am guessing my error maybe in reading images
%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%%%%
freq = 5; %Hz; aquisition frequency
example = '4_68_image'; % input case
mode=2; % which mode to plot and extract DMD from
%%%%%%%%%%%%%%%%%%% SCRIPT %%%%%%%%%%%%%%%%%%%%%
% Import the images from folder. The images are extracted from the Schlieren
% video for wedge_4_68 at Mach 2.5.
data = [];
% if strcmp(example,'cylinder')
directory = '4_68_image';
count = 1;
while exist(sprintf('%s/scene_%0.4d.bmp',directory,count)) ~= 0
data(:,:,count) = imread(sprintf('%s/scene_%0.4d.bmp',directory,count));
count = count +1;
end
r = size(data,1); c = size(data,2);
% Transform the data into column vectors.
data=reshape(data,r*c,size(data,3));
Please suggest possible solution.
The question is, after you run your code and get no output, what value shows up for count ?
What output do you get if you use
ls(directory)
for count it is showing 1.
K>> ls(directory)
'4_68_image' not found.
Ah, so if the directory named 4_68_image does not exist inside the current directory, then how do you expect to read files from it?
Dear Walter,
I was able to read directory
K>> ls(directory)
. scene_0016.bmp scene_0033.bmp scene_0050.bmp
.. scene_0017.bmp scene_0034.bmp scene_0051.bmp
scene_0001.bmp scene_0018.bmp scene_0035.bmp scene_0052.bmp
scene_0002.bmp scene_0019.bmp scene_0036.bmp scene_0053.bmp
scene_0003.bmp scene_0020.bmp scene_0037.bmp scene_0054.bmp
scene_0004.bmp scene_0021.bmp scene_0038.bmp scene_0055.bmp
scene_0005.bmp scene_0022.bmp scene_0039.bmp scene_0056.bmp
scene_0006.bmp scene_0023.bmp scene_0040.bmp scene_0057.bmp
scene_0007.bmp scene_0024.bmp scene_0041.bmp scene_0058.bmp
scene_0008.bmp scene_0025.bmp scene_0042.bmp scene_0059.bmp
scene_0009.bmp scene_0026.bmp scene_0043.bmp scene_0060.bmp
scene_0010.bmp scene_0027.bmp scene_0044.bmp scene_0061.bmp
scene_0011.bmp scene_0028.bmp scene_0045.bmp scene_0062.bmp
scene_0012.bmp scene_0029.bmp scene_0046.bmp
scene_0013.bmp scene_0030.bmp scene_0047.bmp
scene_0014.bmp scene_0031.bmp scene_0048.bmp
scene_0015.bmp scene_0032.bmp scene_0049.bmp
But now the issue is: Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Error in POD_DMD (line 17)
data(:,:,count) =
imread(sprintf('%s/scene_%04d.bmp',directory,count));
Please help as I am not experienced in Matlab.
data(:,:,count) =
rgb2gray(imread(sprintf('%s/scene_%04d.bmp',directory,count))) ;

Sign in to comment.

Answers (1)

Products

Release

R2020b

Asked:

on 10 Jul 2021

Edited:

on 12 Jul 2021

Community Treasure Hunt

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

Start Hunting!