MATLAB Answers

How to make a new folder within another folder?

144 views (last 30 days)
Ibro Tutic
Ibro Tutic on 29 Oct 2015
Commented: Thorsten on 30 Oct 2015
Hi, I need to make a new sub folder within a main folder. When using mkdir(newfolder, newsubfolder) where newfolder is the parent folder where I want the newsubfolder saved, I get syntax errors every time. The full code is attached below.
> In CSVtoMatLab (line 77) Error using mkdir The filename, directory name, or volume label syntax is incorrect.
Error in CSVtoMatLab (line 85) mkdir(newfolder,newsubfolder);
Thanks for any help.
if true
clear all
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Asks for User Inputs %%%%%%%%%%%%%%%%%%%%%%
prompt={'Enter a value for R:','Enter a value for C:','Enter a value for beta:','Enter a value for Life Goal:','Enter a value for N:','Enter a value for m:'};
title='Resonant Dwell Calculator Inputs';
a = str2num(answer{1});
b = str2num(answer{2});
c = str2num(answer{3});
d = str2num(answer{4});
e = str2num(answer{5});
f = str2num(answer{6});
%%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
projectdir = 'C:\Users\it58528\Documents\Dig Test'; %Start here. or name an absolute directory
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%Reliability goals %%%%%%%%%%%%%%%%%%%%%%%%%%%%
R = a;
C = b;
beta = c;
LifeGoal = d;
N = e;
m = f;
%%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
newsubfolder = fullfile(newdir, subfolderi);
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename, ext] = fileparts(thisfile);
data = csvread(thisfile,5,2);
PIN(fileidx).PIN = fileinfo(fileidx).name(1:17);
PIN(fileidx).loadprofile = data(1:15,:);
PIN(fileidx).hours = sum(sum(PIN(fileidx).loadprofile,1));
PIN(fileidx).loadprofilepercent = PIN(fileidx).loadprofile./PIN(fileidx).hours;
PIN(fileidx).loadpercent = data(:,2);
PIN(fileidx).RPM = data(16,:);
loadprofilecolumn = find(PIN(fileidx).RPM>Resonance);
xSpeed = PIN(fileidx).RPM(loadprofilecolumn(1)-1);
newsubfolder = fullfile(newdir, subfolderi);
newfilename = fullfile(newsubfolder, filenamei);
end %files within subfolder
end %subfolders within folder
end %folders end


Stephen Cobeldick
Stephen Cobeldick on 29 Oct 2015
Please tell us the complete error message, which means all of the red text. The error message has useful information that help use to understand what is happening when your code runs.
Adam on 29 Oct 2015
Have you put breakpoints in?
This kind of thing is usually far quicker to find the problem with by just putting in a breakpoint, looking on the command line at the components you are using and passing to mkdir and seeing the problem than waiting for answers on a forum.
mkdir(newfolder, newsubfolder)
syntax works fine in a general case if the arguments you give it are sensible - i.e. an existing parent folder and a valid string for the subfolder.
Obviously you will need to have write access in the folder too though.

Sign in to comment.

Accepted Answer

Thorsten on 29 Oct 2015
Just use one argument:
And in the for fileidx = 1 : length(fileinfo) loop, get rid of the lines
newsubfolder = fullfile(newdir, subfolderi);
This has to be done just once for each subfolder, not once for each file in a subfolder.


Show 1 older comment
Thorsten on 29 Oct 2015
You have the first loop that should create the four folder using
and you have the inner loop that creates the 200 subfolders using
I would add a disp(newfolder) and disp(newsubfolder) in front of every mkdir command to see what is actually created.
Ibro Tutic
Ibro Tutic on 29 Oct 2015
Yea, that works a little. It creates the two folders within the specified directory. So within the folder 'Test' I get two files, 'Bla' and 'CSV Data'. And within the CSV Data is the data I am saving. Now what I need to happen is the 'CSV Data' folder to be inside of the 'Bla' folder. But this creates both folders inside of the directory, which is not what I want.
I assume I could somehow edit the second directory to point into the 'Bla' folder, so the 'CSV Data' folder is saved within it? I am not sure how to go about that without making it user friendly in the sense that you don't need to edit the directory within the code.
Thanks for the help.
Thorsten on 30 Oct 2015
Must read
newsubfolder = fullfile(newfolder, subfolderi);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by