How to load multiple csv files and save them after converting into arff files?

Hello Friends,
I have several csv files, all stored in path 'C:\Users\Name\Documents\MATLAB\*.csv'. I want to loop one by one each file in order, convert them into arff files, and save them 'C:\Users\Name\Documents\MATLAB\*.arff'

 Accepted Answer

datadir = 'C:\Users\Name\Documents\MATLAB';
d = dir(fullfile(datadir, '*.csv'));
for i=1:length(d)
filename = fullfile(datadir, d(i).name);

5 Comments

I stopped copying at the "%Load csv file." part as it was no important for the point.
It helps if you specify what order you want them in. Perhaps you want http://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
If you want to use an output name corresponding to the input name then
outfile = [filename(1:end-2) 'arff'];
outfile = [filename(1:end-2) 'arff'];
saverFile = javaObject('java.io.File', outfile);
It would be much more robust to use regexprep rather than indexing and string concatenation, something like this:
>> name = 'MyFile.csv';
>> regexprep(name,'\.csv$','.arff')
ans =
MyFile.arff

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!