read multiple excel file and apply process on it using matlab
9 次查看(过去 30 天)
显示 更早的评论
i have multiple excel files in D:\ directory these files name starting with "gt" characters i need to delete all empty rows in these files in , Sheet1 only and store processed files in other names ,
Q :\ The following code has been successfully deleted all empty rows but from single file , how we can applied it to all files in D:\ directory and their names start with "gt" characters ? the following code which i mention above ...
projectdir = 'D:'
FileNames = dir(fullfile(projectdir, 'gt*.xls'));
j=1;
for i=1:length(FileNames)
FileToLoad = fullfile(projectdir, FileNames(i).name);
[~, ~, data] = xlsread(FileToLoad,'Sheet1');
data(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x), data)) = {''};
ii = 1;
while true
try
if any(strcmp('', data(ii,:))) % find rows with empty cell
data(ii,:) = []; % remove the row
else
ii = ii+1;
end
catch
break % When the process goes beyond the end, stop the loop.
end
end
outputfile = fullfile(projectdir, 'result{i}.xls');
xlswrite(outputfile, data);
end
i hope to get output like :
gt1.xls file store as result1.xls file ;
gt2.xls file store as result2.xls file ;
and so on ....
0 个评论
采纳的回答
Walter Roberson
2015-8-18
That code appears to loop over the desired files, but it always writes the output to the same file 'gt.xls'. You may wish to use
xlswrite(FileToLoad, data)
2 个评论
Walter Roberson
2015-8-18
Make sure you specify the path for the file when you open it. Using fullfile() is usually a good idea.
projectdir = 'D:'
FileNames = dir(fullfile(projectdir, 'gt*.xls'));
FileToLoad = fullfile(projectdir, FileNames(i).name);
...
outputfile = fullfile(projectdir, 'gt.xls');
xlswrite(outputfile, data);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!