Borrar filtros
Borrar filtros

Problem in loading a set of files

1 visualización (últimos 30 días)
Shraddha Joshi
Shraddha Joshi el 28 de Abr. de 2016
Editada: Stephen23 el 28 de Abr. de 2016
In order to load a set of .mat files, I have used the instructions as below,
myfolder='C:\Documents\MATLAB';
filepattern=fullfile(myfolder,'*.mat');
dinfo=dir(filepattern);
filenames={dinfo.name};
nfiles=length(filenames);
results=cell(nfiles, 1);
for K=1:nfiles
thisfile=filenames{K};
matstruct=load(thisfile);
ecgs=matstruct.val(1,:);
for rownum=1:size(ecgs,1)
ecg=ecgs(rownum,:);
results{K,rownum}=pan_tompkin(ecg,fs,gr);
end
end
Suppose if I am having 25 files to be loaded in 'myfolder' above. By using the above instructions, only one signal is loaded 24 times giving its output 24 times and another signal is loaded 25th time. Whereas I want all the 25 signals to be loaded one after the other once. Could you tell me where exactly I am going wrong? Thanks in Advance.
  1 comentario
Stephen23
Stephen23 el 28 de Abr. de 2016
Editada: Stephen23 el 28 de Abr. de 2016
Your code looks okay, there is no glaring cause of why it would not loop correctly over all of your data files. Have you checked the data files themselves? Have they been correctly named?
Print the contents of filenames: does it have all different filenames? Try this:
numel(unique(filenames))
and check that the answer is 25. Then inside your loop add
disp(thisfile)
Perhaps the cause is the indexing around the ecgs variable, where there is possibly some confusion about row/column indexing:
ecgs = matstruct.val(1,:); % all columns ?
for rownum = 1:size(ecgs,1) % now all rows ?
ecg = ecgs(rownum,:); % each row... ?
results{K,rownum}= ... % placed in each column...
Perhaps you need to check that indexing.

Iniciar sesión para comentar.

Respuestas (1)

KSSV
KSSV el 28 de Abr. de 2016
You may use the following
matfiles = dir('*.mat') ; % Get the matfiles in present folder (matfiles will be structure)
Nfiles = length(matfiles) ; % Total number of files
for i = 1:Nfiles % loop for each file
load(matfiles(i).name) ; % load the file
% d0 what you want
end
  1 comentario
Stephen23
Stephen23 el 28 de Abr. de 2016
@Dr. Siva Srinivas Kolukula: the OP's used a much better concept of loading into a variable rather than loading directly into the workspace. We should all learn from such good examples:
for i = 1:Nfiles
...
matstruct = load(matfiles(i).name);
...
end

Iniciar sesión para comentar.

Categorías

Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by