index exceeds matrix dimensions?

12 Ansichten (letzte 30 Tage)
Jessica Guzmán López
Jessica Guzmán López am 9 Feb. 2018
Beantwortet: Walter Roberson am 9 Feb. 2018
Hi,
I have this code but there is an error in the matrix dimensions. If someone can help?
thank you,
clear all
close all
clc
framesPerSec=4
movie_name=('images')
aviobj = avifile(movie_name,'fps',framesPerSec) ; %Initialize .avi file
dname=('/Users/JGL/Desktop/Julio_EEG/images')
%%Set up basic file name path to read
top_file = [dname '/'] ; %Set up main database to open and look inside
ls_top_file = ls(top_file) ; %List Files inside main folder
c = cellstr(ls_top_file) ; %Turn cells from ls function into strings
cc = c(3:length(c)) ; %Set up a matrix without the . and .. produces by the ls function
S = size(cc) ; %Find the size of matrix containing names of files inside of main database
a = 1 ; %This counter is set to 3 to account for the . and .. at the begining of each matrix created by "ls"
while a <= S(1)
close all
file = char(cellstr([top_file char(cc(a))])) ; %File to be operated on
data_n = char(cc(a))
file_name = char(cc(a)) ;
figure
fileToRead2 = [dname '/' file_name] ;
imshow((fileToRead2))
axis tight
hold off
h = getframe(gcf) ; % get current figure handle
aviobj = addframe(aviobj,h) ; % add frame to .avi file
a = a+1 ;
end
%%Finish Movie
h = getframe(gcf) ; % get current figure handle
aviobj = addframe(aviobj,h) ; % add frame to .avi file
aviobj = close(aviobj)
  1 Kommentar
Adam
Adam am 9 Feb. 2018
Simplest option is to use the stop/pause on errors option in the breakpoints menu of the editor. Then it should be trivial to find.
Just staring at a large amount of (mostly unformatted) code is extremely difficult for anyone else to instantly see where the error would be when we have no idea which line it is on.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jessica Guzmán López
Jessica Guzmán López am 9 Feb. 2018
Bearbeitet: Walter Roberson am 9 Feb. 2018
Here, I would like to use this code in order to convert .jpg images to a video in matlab. Here is the code again. Hope this time is better to see it. The error in line 33, where is said "end" in the code.
Many thanks,
jessica
clear all
close all
clc
framesPerSec=4
movie_name=('images')
aviobj= avifile(movie_name,'fps',framesPerSec); %Initialize .avi file
dname=('/name of the file')
%%Set up basic file name path to read
top_file= [dname '/']; %Set up main database to open and look inside
ls_top_file= ls(top_file); %List Files inside main folder
c= cellstr(ls_top_file); %Turn cells from ls function into strings
cc = c(3:length(c));
S = size(cc);
a= 1;
while a <= S(1)
close all
file= char(cellstr([top_file char(cc(a))])); %File to be operated on
data_n= char(cc(a))
file_name= char(cc(a));
figure
fileToRead2 = [dname '/' file_name];
imshow((fileToRead2))
axis tight
hold off
h= getframe(gcf) ; % get current figure handle
aviobj= addframe(aviobj,h); % add frame to .avi file
a = a+1 ;
end
%%Finish Movie
h= getframe(gcf); % get current figure handle
aviobj= addframe(aviobj,h); % add frame to .avi file
aviobj= close(aviobj); %Close .avi file

Walter Roberson
Walter Roberson am 9 Feb. 2018
You have
ls_top_file= ls(top_file); %List Files inside main folder
c= cellstr(ls_top_file); %Turn cells from ls function into strings
cc = c(3:length(c));
The output of ls() is a single character vector with embedded newlines. cellstr() applied to that would be the smae as {ls_top_file} -- you would get out a 1 x 1 cell that contained a character vector with embedded newlines. c(3:length(c)) would be an empty cell because you would be attempting to take c(3:1) and 3:1 is the empty vector.
If somehow your c was a cell array of character vectors, one file name per cell, then taking 3:end is a common technique for attempting to skip the '.' and '..' entries. It is, however, incorrect. The order of files returned by ls() is not defined by MATLAB or even by the operating system: it is at best defined by which-ever program is run to implement 'ls' (on OS-X and Linux that would be /bin/ls ), and more likely is defined (or left undefined) by the file system. In practice, on MS Windows with NTFS file systems, any file whose name begins with any of !"#$%&'()*+,- will sort to before '.' and '..' . In practice, on OS-X and Linux, ls() will return multiple columns and will not distinguish between the spaces separating columns and a space that happens to be part of a file name.
file= char(cellstr([top_file char(cc(a))])); %File to be operated on
just makes my head hurt.
Replacement code:
dinfo = dir(dname);
dinfo([dinfo.isdir]) = []; %remove all folders, including . and ..
cc = fullfile(dname, {dinfo.name});
Now cc is a cell array of character vectors, each of which is a full qualified file name.
Then inside your loop,
file = cc{a};

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by