Converting multiples excel files to .mat

Hi all, I need to convert multiple xlsx files to .mat. The directory contains 4 folders which contain about 200 folders each which contain about 4 excel files each. How can I get MatLab to go this directory and convert every .xlsx file to .mat? I know how to do it for one file, but that doesn't help very much when I have to do it for almost a thousand files.
So far I have this:
files = dir('*.csv');
for file = files'
csv = csvread(file.name);
% Do some stuff
end
Now I need it to read every .csv file and save the data as .mat. Would this something like this work?
files = dir('*.csv');
for file = files'
csv = csvread(file.name);
save = (file.name);
end
Will this go through every folder in the directory and create .mat copies of the .csv data?
Any help appreciated.

 Accepted Answer

8 Comments

That doesn't really help me. I need it to go to the directory and open every file and convert them to .mat. The link you provided only helps if all of the .csv files are already in the same directory. For me to put every .csv file in the same directory it would take days. It got me started, but I still need to figure out how to get MatLab to open other folders within the specified directory.
The FAQ are not intended to be code to cover all possibilities: they are intended to be code showing you techniques that you can apply.
Using only the techniques shown in the FAQ, together with fileparts(), you can create this:
projectdir = '.'; %Start here. or name an absolute directory
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..})); %remove directories . and ..
for subfolderidx = 1 : length(subfolderinfo)
thissubfolder = fullfile(thisfolder, subfolderinfo(subfolderidx).name);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
for fileidx = 1 : length(fileinfo)
thisfile = fullfile(thissubfolder, fileinfo(fileidx).name);
[filepath, basename, ext] = fileparts(thisfile);
outputfile = fullfile(filepath, [basename '.mat']);
SaveVariableNameGoesHere = csvread(thisfile); %or as appropriate to read file
save(outputfile, 'SaveVariableNameGoesHere'); %save as .mat
end %files within subfolder
end %subfolders with folder
end %folders
Thanks for the help. Where will it save the converted files? Within the subfolders themselves or in another directory? I tested it with one file and could not find the converted .mat file.
It will save in the same directory as the individual .csv file, just replacing the .csv extension with .mat
Will it keep the file name the same as the original .csv file and just add a .mat version of it? I don't want it to replace the .csv file, I just want a .mat version of it available for use if needed.
It will not remove the .csv, just add a .mat copy of it in the same location the .csv already is.
Thanks for all the help. One more question, how would I modify where I want the files saved? I would like to put them in a separate folder labeled with a time stamp so I know when all the files were converted, so when I do future conversions, I know what data is the most up to date. Again, thanks a ton.
Also, the CSV files I am reading contain text, so cvsread will not work. I am trying to implement textscan, but I am having troubles with how to set up the inputs. textscan(file,'?'). I noticed people using %f, %d, delimiter and stuff like that but I am not sure how to utilize these things to have it scan the .csv files correctly.

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!