MATLAB: Create subfolders in the desired way

subfolders loop if

Hi community! I would appreciate your contribution to the following.
I have created the directory I want (the first of which is C:\Users\dparliari\Desktop\OutputEv\Airport\Temperature) using the following lines:
output_path='C:\Users\dparliari\Desktop\OutputEv'
vars={'Temperature'; 'Relative humidity'};
for m=1:size(vars,1)
if(~exist([output_path,'\',namestr,'\',strrep(vars{m},' ','_'),'\'],'dir'))
mkdir([output_path,'\',namestr,'\',strrep(vars{m},' ','_'),'\'],'dir')
end
end
Now I want to create a subfolder based on the year in this way: C:\Users\dparliari\Desktop\OutputEv\Airport\Temperature\2015
I guess it must be something like:
output_path='C:\Users\dparliari\Desktop\OutputEv'
vars={'Temperature'; 'Relative humidity'};
years = {'2015'; '2019'};
for m=1:size(vars,1)
if(~exist([output_path,'\',namestr,'\',strrep(vars{m},' ','_'),'\'],'dir'))
mkdir([output_path,'\',namestr,'\',strrep(vars{m},' ','_'),'\'],'dir')
for k = 1:size(years,1)
if(~exist([I HAVE NO IDEA!!))
mkdir([ALSO NO IDEA!!)
end
end
end
Any ideas please??

Best Answer

As Per isakson said, use fullfile instead of building paths manually. fullfile automatically inserts the correct path separator for whichever OS your code is running on.
I would do it like this
output_path='C:\Users\dparliari\Desktop\OutputEv';
vars={'Temperature'; 'Relative humidity'};
years = {'2015'; '2019'};
[vv, yy] = ndgrid(vars, years); %note that this syntax is not officially supported. If it's a problem use
%[vv, yy] = ndgrid(1:numel(vars), 1:numel(years)); vv = vars(vv); yy = years(yy);
allfolders = fullfile(output_path, 'Airport', vv, yy);
for fidx = 1:numel(allfolders)
if ~exist(allfolders{fidx}, 'dir')
mkdir(allfolders{fidx});
end
end