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.

 채택된 답변

Image Analyst
Image Analyst 2014년 5월 12일

1 개 추천

If the csv file is "blank" will the file size be zero? If so, you can use dir() to check the file sizes.

댓글 수: 11

Franchesca
Franchesca 2014년 5월 12일
Yes the file size is zero, so if I use dir() and check the file size how do I skip this file?
dpb
dpb 2014년 5월 12일
편집: dpb 2014년 5월 12일
These file names must have been created by OS operations, then, not closing an Excel file w/ an empty sheet and saving as csv. When I just tried that here, I got--
>> d=dir('ex*.csv')
d =
name: 'excel.csv'
date: '12-May-2014 14:39:02'
bytes: 2
isdir: 0
datenum: 7.3573e+05
>>
It's fewer bytes than I expected and that would be good enough, I guess, as all 2 bytes could be would be a \n or the like. I wasn't thinking of .csv earlier, though, was thinking a .xls that I'd expect has more overhead (altho I haven't checked).
Anyway, my first reaction is "Awww, c'mon...at least try a little here..." :(
if d(i).bytes==0,continue,end
Franchesca
Franchesca 2014년 5월 12일
This still gives me an error...
Error using xlsread (line 247) File Trial6.CSV not in Microsoft Excel Format.
Error in MatlabCoursework (line 18) mydata{i} = xlsread(myfilename); % import files into mydata
dpb
dpb 2014년 5월 12일
You didn't put it in the right place then or the size isn't actually zero.
You've got to show your work; we can't guess at what you did...
Franchesca commented,
I am a novice to Matlab and am trying to understand and use the answers people are giving me however in this case I tried the suggestion, used the help function in Matlab but still was unsure so asked again how to solve my problem. I feel patronised by this answer and don't think I should be spoken to like that when all I'm doing is try to understand.
dpb's code is correct. You should have
d=dir('trial*.csv');
for i=1:length(d)
try
if d(i).bytes == 0
continue; % Skip this one file because it is empty.
end
mydata{i} = xlsread(d(i).name); % import files into mydata
catch
disp([d(i).name ' read failed'])
end
end
if d(i).bytes < 10; continue; end
Franchesca
Franchesca 2014년 5월 12일
Thank you!!
dpb
dpb 2014년 5월 12일
@Franchesca...not intended to be condescending; simply chiding to "show your work". You didn't show any attempt to use the file size at all; if you had tried something at it failed should have shown that as you should have shown the code wherein you still had a problem after being shown the proper construct to use. I've taught a lot over the years and my general approach has always been to try to coach rather than show; hint rather than solve because I believe it leads to better retention/learning of both the subject and how to learn to grow in capabilities on own more quickly than if just given solutions.
Image Analyst
Image Analyst 2014년 5월 12일
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 2014년 5월 12일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2014년 5월 12일
편집: dpb 2014년 5월 12일

2 개 추천

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
Franchesca 2014년 5월 12일
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 2014년 5월 12일
편집: dpb 2014년 5월 12일
No it belongs at end to close the message string. Corrected.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

질문:

2014년 5월 12일

댓글:

dpb
2014년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by