Checkign if a spreadsheet in excel file is empty?
Show older comments
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
Answers (1)
Andrei Bobrov
on 28 May 2012
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
Categories
Find more on Spreadsheets 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!