MATLAB: How to export Excel files to the specific folder

directoryexportfolderfullfilepathsave

I have the below code:
for p = 1:numel(C)
filename = C{p}{1,2};
if ~isempty(filename{:})
sprintf('%s.xlsx',filename{:})
writetable(C{p},sprintf('%s.xlsx',filename{:}))
end
end
I want to save all excel files generated in this code to "E:\AZAR\xlsx files 1989-2018" instead of the current folder. does anyone have an idea for how to do it?
Thank you all.

Best Answer

Use the fullPath = fullfile(path, filename) function to create full paths to files. This offers a number of very important benefits over simply concatenating strings to produce a full path. From the documentation,
  • fullfile inserts platform-dependent file separators where necessary (on Windows platforms it's a backslash \).
  • and it replaces all forward slashes (/) with backslashes (\) on Windows.
  • It also smartly concatenates string by collapsing inner repeated file separators.
directory = 'E:\AZAR\xlsx files 1989-2018';
filename = 'data.xlsx';
writetable(C{p},fullfile(directory,filename));