How do I skip a blank file?

21 views (last 30 days)
Franchesca
Franchesca on 12 May 2014
Commented: dpb on 12 May 2014
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.

Accepted Answer

Image Analyst
Image Analyst on 12 May 2014
If the csv file is "blank" will the file size be zero? If so, you can use dir() to check the file sizes.
  11 Comments
Image Analyst
Image Analyst on 12 May 2014
Actually if it's a csv file I don't know why you're using xlsread() to read it instead of csvread(). xlsread will take much longer because it actually has to launch Excel (I believe).
dpb
dpb on 12 May 2014
@IA, this is the same person with whom we spent several days just last week trying to use csvread on files with header content besides the data...let's not rehash that ground.
I suggested either textscan or textread would be the logical alternatives w/ xlsread the last choice w/ the idea she would be going back to the underlying .xls workbook files, not .csv saved from Excel.

Sign in to comment.

More Answers (1)

dpb
dpb on 12 May 2014
Edited: dpb on 12 May 2014
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 Comments
Franchesca
Franchesca on 12 May 2014
I recieve this answer:
Error using disp Too many input arguments.
Error in ImportCSV (line 17) disp([d(i).name],'read failed')
A square bracket was missed so I added it after name I don't know if this is correct
dpb
dpb on 12 May 2014
Edited: dpb on 12 May 2014
No it belongs at end to close the message string. Corrected.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!