MATLAB: Converting .xls and .xlsx to .csv

convertcsvforifloopwhilexlsxlsx

Hey guys,
I was wondering if there was a way I could ask a user if their file is .csv, and if not, I would sent it through a loop to have it converted to .csv. Is there any way I could do that?
Thanks!

Best Answer

Why not assume the extension is correct and process it if necessary?
[folder, baseFileName, extension] = fileparts(filename);
if strcmpi(extension, '.xlsx')
numbers = xlsread(filename);
csvFileName = strrep(filename, '.xlsx', '.csv');
csvFileName = strrep(csvFileName, '.xls', '.csv');
csvwrite(csvFileName, numbers);
end