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
답변 (1개)
Andrei Bobrov
2012년 5월 28일
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
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!