Problem in loading a set of files

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2016-4-28
编辑:Stephen23 2016-4-28
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.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2016-4-28
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 个评论
Stephen23
Stephen23 2016-4-28
@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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by