read multiple excel file and apply process on it using matlab

1 view (last 30 days)
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 ....

Accepted Answer

Walter Roberson
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
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);
ahmed obaid
ahmed obaid on 19 Aug 2015
thank you sir , i have modified my code but also not write my interested output its write only 1 excel file , hope to get the following output
gt1.xls file store as result1.xls file ;
gt2.xls file store as result2.xls file ;
and so on ....
please sir can you modified and submit the full code .. thanks again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!