How do we convert multiple csv files to execl file in matlab ?

Dear experiences ...
i have multiple csv files in certain folder ("D:\original")(100+), i need to convert these files from csv to xls files and store it in ("D:\master").. i have used the following code but its give me an error in xlsread ..
Error using xlsread (line 128)
XLSREAD unable to open file 'c.csv'.
File 'C:\Program Files\MATLAB\MATLAB Production Server\R2015a\bin\c.csv' not found.
the code is in the following:
orginal='D:\original'
d=dir([original '\*.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
thanks

 Accepted Answer

orginal='D:\original'
d=dir([orginal '\*.csv']); %Spelling mistake here
files={d.name};
for k=1:numel(files)
filename=files{k};
fullname = fullfile(orginal,filename );%Getting the fullfile name
[~,~,b] = xlsread(fullname) ;
new_name=strrep(fullname,'csv','xls')
xlswrite(new_name,b);
end

3 Comments

thaks for your help...
i need to write new excels files to master folder in the following path ('D:\master), otherwise old csv files in orginal must be deleted .. i'm using the following code buts also show me an errors ..
orginal='D:\original'
d=dir([orginal '\*.csv']); %Spelling mistake here
files={d.name};
for k=1:numel(files)
filename=files{k};
fullname = fullfile(orginal,filename );%Getting the fullfile name
[~,~,b] = xlsread(fullname) ;
new_name=strrep(fullname,'csv','xlsx')
new_folder='D:\master'
new_file=fullfile(new_folder,new_name)
xlswrite(new_file,b);
end
and its show me the following errors..
Error using xlswrite (line 219)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: SaveAs method of Workbook class failed
Help File: xlmain11.chm
Help Context ID: 0
masterdir = 'D:\master'
xlswrite( fullfile( masterdir, new_name), b)
[filedir, basename, ext] = fileparts(filename);
new_file = fullfile( masterdir, [basename '.xlsx']);
and you can get rid of some of your other lines like
new_name=strrep(fullname,'csv','xlsx')

Sign in to comment.

More Answers (0)

Products

Asked:

on 24 May 2017

Edited:

on 24 May 2017

Community Treasure Hunt

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

Start Hunting!