Hi!
I have a folder containing many excel files ( more than 200) in csv format . I need to write all these files in xls format.
Can this be done in matlab . Can anyone please help.
Thanks in advance

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 25 Oct 2013
Modifié(e) : Azzi Abdelmalek le 25 Oct 2013
yourfolder='E:\matlab'
d=dir([yourfolder '\*.csv']);
files={d.name};
for k=1:numel(files)
old_name=files{k};
[~,~,b] = xlsread(old_name) ;
new_name=strrep(old_name,'csv','xls')
xlswrite(new_name,b);
end

7 commentaires

avantika
avantika le 25 Oct 2013
Hi!
Thanks
But is there a way to save these xls files to new folder?
Azzi Abdelmalek
Azzi Abdelmalek le 25 Oct 2013
Modifié(e) : Azzi Abdelmalek le 25 Oct 2013
yourfolder='E:\matlab'
d=dir([yourfolder '\*.csv']);
files={d.name};
for k=1:numel(files)
old_name=files{k};
[~,~,b] = xlsread(old_name) ;
new_name=strrep(old_name,'csv','xls')
new_folder='E:\another'
new_file=fullfile(new_folder,new_name)
xlswrite(new_file,b);
end
avantika
avantika le 28 Oct 2013
thanks a lot!
Image Analyst
Image Analyst le 2 Nov 2013
Avantika, you forgot to accept it, so I did it for you.
Robert Jones
Robert Jones le 12 Nov 2013
great info. Q: how to merge the *.csv files as sheets of ONE excel file?
More correct would be
yourfolder = 'E:\matlab'
d = dir(fullfile(yourfolder, '*.csv'));
files = {d.name};
for k=1:numel(files)
old_name = files{k};
[~,~,b] = xlsread( fullfile(yourfolder, old_name)) ;
new_name = strrep(old_name,'csv','xls')
new_folder = 'E:\another'
new_file = fullfile(new_folder, new_name)
xlswrite(new_file,b);
end
Onur ALPAY
Onur ALPAY le 8 Déc 2020
Hi,
I am using MATLAB 2019a version and it looks there is no problem with version. I try this code but I realized that the below code doesn't work. It gives me the same file name. Do you have any experiance about that?
new_name = strrep(old_name,'csv','xls')

Connectez-vous pour commenter.

Plus de réponses (2)

Fabian Neira
Fabian Neira le 1 Juin 2018

0 votes

I got the following error "File name must be a character vector" when using the code.
Lotan Jackson
Lotan Jackson le 25 Août 2020
Hi everyone, I know this is an old question but I had to adjust things for either the newest version or being on a newest matlab version. Here's the code. I placed all the files in the folder with the script.
file = dir('*.csv');
s= size(file,1);
for i= 1:s
Data = readtable(file(i).name);
filename=file(i).name;
filename= filename(1:end-5);
writetable(Data,[filename '.xls']);
end;
Hope this helps anyone in the future. :)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by