read multiple excel file and apply process on it using matlab
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ahmed obaid
le 18 Août 2015
Commenté : ahmed obaid
le 19 Août 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 commentaires
Réponse acceptée
Walter Roberson
le 18 Août 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 commentaires
Walter Roberson
le 18 Août 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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!