read multiple excel file and apply process on it using matlab
1 view (last 30 days)
Show older comments
ahmed obaid
on 18 Aug 2015
Commented: ahmed obaid
on 19 Aug 2015
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 Comments
Accepted Answer
Walter Roberson
on 18 Aug 2015
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 Comments
Walter Roberson
on 18 Aug 2015
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);
More Answers (0)
See Also
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!