How to export Excel files to the specific folder?

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.

 Accepted 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));

6 Comments

Dear Adam Danz,
Thank you for your answer. According to your answer I change the code to:
directory = 'E:\AZAR\xlsx files 1989-2018\';
for p = 1:numel(C)
filename = C{p}{1,2};
if ~isempty(filename{:})
sprintf('%s.xlsx',filename{:})
writetable(C{p},sprintf('%s.xlsx',dircetory, filename{:}))
end
end
Is this correct?
after doing that, the excel files neither export in the current folder nor on E:\AZAR\xlsx files 1989-2018!
Thank you
I don't see my solution implemented in your code. Where is fullfile()?
Oh yes you right. I change it to:
out_dir='E:\AZAR\Prepared xlsx files 1989-2018';
for k = 1:numel(C)
filename = C{k}{1,2};
if ~isempty(filename{:})
baseFileName = sprintf('%s.xlsx',filename{:});
fullFileName = fullfile(out_dir, baseFileName);
writetable(C{k},sprintf('%s.xlsx',filename{:}))
end
end
But I don't know why the output files still saving in the current folder?
Take a moment to look at what the function is doing; look at its outputs.
Hint: fullFileName should be the 2nd input to writetable exactly as shown in my answer.
oh, it's done. really appreciate. I edited it and it's worked well.
out_dir='E:\AZAR\Prepared xlsx files 1989-2018';
for k = 1:numel(C)
filename = C{k}{1,2};
if ~isempty(filename{:})
baseFileName = sprintf('%s.xlsx',filename{:});
fullFileName = fullfile(out_dir, baseFileName);
writetable(C{k},fullfile(out_dir, baseFileName))
end
end
Thank you for your time and help :)
No problem!
Note the small improvement made below ( see arrow).
out_dir='E:\AZAR\Prepared xlsx files 1989-2018';
for k = 1:numel(C)
filename = C{k}{1,2};
if ~isempty(filename{:})
baseFileName = sprintf('%s.xlsx',filename{:});
fullFileName = fullfile(out_dir, baseFileName);
writetable(C{k},fullFileName) %<--------------
end
end

Sign in to comment.

More Answers (1)

You can find the answer in the documentation for the writetable function. You must simply format your filename to be E:\AZAR\xlsx files 1989-2018\yourname.xlsx, for example using the strcat function, like;
strcat('E:\AZAR\xlsx files 1989-2018',sprintf('%s.xlsx',filename{:})));

2 Comments

Dear Jakob B. Nielsen
I tried this code that you wrote, but I don't know why it didn't work for me.
Thank you
It looks like the backslash may be missing between the path and filename.
Another possibility is that your platform expects a different path format.
These problems are all solved by using the fullfile() function.

Sign in to comment.

Asked:

BN
on 15 Jan 2020

Commented:

on 15 Jan 2020

Community Treasure Hunt

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

Start Hunting!