Checkign if a spreadsheet in excel file is empty?

Hi.
I am trying to read data from excel spreadsheets, some files will have multiple spreadsheets with data while others wil have only one. The problem is that when there is an empty sheet the program sctript stops. Is there a way to check if the sheet is empty with an if command?
Thanks Sample Code
%Enter folder path with XRD files
source_dir = 'C:\Users\Desktop\XRD Process\';
source_files = dir(fullfile(source_dir, '*.xls'));
for i = 1:length(source_files)
xlsfile=strcat(source_dir,source_files(i).name)
[status,sheets]= xlsfinfo(xlsfile);
NumSheets=length(sheets)
%this section will loop through each sheet
for current_sheet=1:NumSheets
xrddata(current_sheet).name=sheets{current_sheet};
[num,txt,raw] = xlsread(xlsfile,xrddata(current_sheet).name);
xrddata(current_sheet).two_theta=num(:,2);
xrddata(current_sheet).cts=num(:,3);
xrddata(current_sheet).RI=(num(:,3)/max(num(:,3)));
end
end

Réponses (1)

try this is code
%Enter folder path with XRD files
source_dir = 'C:\Users\Desktop\XRD Process\';
source_files = dir(fullfile(source_dir, '*.xls'));
k = 0
xrddata = struct('name',[],'two_theta',[],'cts',[],'RI',[]);
for i = 1:length(source_files)
xlsfile=strcat(source_dir,source_files(i).name)
[status,sheets]= xlsfinfo(xlsfile);
NumSheets=length(sheets)
%this section will loop through each sheet
for current_sheet=1:NumSheets
k = k + 1;
xrddata(k).name=sheets{current_sheet};
[num,txt,raw] = xlsread(xlsfile,xrddata(k).name);
if ~isempty(num)
xrddata(k).two_theta=num(:,2);
xrddata(k).cts=num(:,3);
xrddata(k).RI=(num(:,3)/max(num(:,3)));
end
end
end

Question posée :

le 28 Mai 2012

Community Treasure Hunt

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

Start Hunting!

Translated by