How do I skip a blank file?
이전 댓글 표시
I am importing a mass of files and am processing them, however a short way in there is a blank file so it imports but then the calculations cannot be performed on it and the loop stops.
How do I define if the file is blank either delete or ignore the file then carry on to import the rest of the data?
Below is my script:
%% Import data
numfiles = 10; % number of excel files mydata=cell(numfiles,1); % defining size of mydata
for i=1:length(mydata) % loop to import mutliple excel files
myfilename = sprintf('Trial%i.csv', i); % define file name
mydata{i} = xlsread(myfilename); % import files into mydata
%% Perfrom Calculations
%%Define variables
a= 9.81; % acceleration
fps = 250; % frames per second
%%Calculate Jump Height
no_of_zeros = size(mydata{i,1},1) - nnz(mydata{i,1}(:,5)); %number of zeros
no_of_frames = (no_of_zeros/4); % number of frames
time = ((no_of_frames/fps)/2); % time up
jumph(i,1)=((a*(time*time))/2); % jump height
%%Calculate peak power
loc = find(mydata{i,1}(:,5)==0); loc = loc(1); % returns all the occurences of zero from column 5, then extracts the first
peakp(i,1) = nanmin (mydata {i,1}(1:loc,5)); % finding peak power from E1 to index cell
%%Calculate average power
avp(i,1) = nanmean (mydata {i,1}(1:loc,5)); % finding average power in column 5
%%Plot Results
figure(1);
subplot (1,3,1);
plot (avp);
%subplot (1,3,2);
%plot (b,c);
%subplot (1,3,3);
%plot (a,c);
title 'Random Graphs'
end
This is the error:
Error using xlsread (line 247) File C:\Users\Caz\Documents\MATLAB\coursework\Trial6.CSV not in Microsoft Excel Format.
Error in MatlabCoursework (line 15) mydata{i} = xlsread(myfilename); % import files into mydata
On opening the Trial6 document outside Matlab it is blank.
채택된 답변
추가 답변 (1개)
Most solid answer I think would be to wrap the read in a try...catch block because I'm thinking there's virtually no way from the directory listing to be absolutely sure the file is empty just from the returned size because Excel stores hidden stuff besides the data. If it were a stream file, then it would return zero bytes but that's not the case w/ Excel (or most applications, actually).
doc try % and friends for details
In rough outline, it would look something like--
d=dir('trial*.csv');
for i=1:length(d)
try
mydata{i} = xlsread(d(i).name); % import files into mydata
catch
disp([d(i).name 'read failed'])
end
end
Again, as I think we've discussed before, using dir is much simpler than building dynamic filenames.
ADDENDUM
With the size test, you should be able to dispense with the try...catch block, but it'll always catch you (so to speak :) ) if something else bad happens if leave it as IA did. W/O it would just be (w/ John's bounding size test if, as my test showed, file size isn't identically zero), it's just minimally
d=dir('trial*.csv');
for i=1:length(d)
if d(i).bytes<10,continue,end % skip empty file
mydata{i} = xlsread(d(i).name); % import if not
end
This version will just silently ignore the (hopefully completely) empty ones. I've used my stylistic preference of the if...end block all on one line when it does nothing but serve as a replacement for GOTO as in this case. There's no meat in a body containing just continue
To keep the information on the bum ones,
d=dir('trial*.csv');
for i=1:length(d)
if d(i).bytes>10
mydata{i} = xlsread(d(i).name); % import files into mydata
else
disp([d(i).name 'read failed'])
end
end
Note we've switched the size test direction to keep instead of reject.
댓글 수: 2
Franchesca
2014년 5월 12일
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!