MATLAB: How to save plots “.pdf” in a folder generated by mkdir function

mkdirsaveas

I want save my plots in a folder created by mkdir. I am to plot from multiple excel files and save with the excel file name in a folder name as Newfolder. The code generates folder "Newfolder" but save the plots in Parent folder, not in Newfolder. How can I change my code to save the plots in Newfolder. my code looks like this.
mkdir('Newfolder'); saveas(gcf, [basename '.pdf']);

Best Answer

outputdir = 'NewFolder';
if ~exist(outputdir, 'dir')
mkdir(outputdir);
end
saveas(gcf, fullfile(outputdir, [basename '.pdf'])