Problem in loading a set of files
    조회 수: 2 (최근 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
      
      
 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
      
      
 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
      
      
 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 Center 및 File Exchange에서 AI for Signals에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!