MATLAB Answers

how to create a new folder mkdir, but naming that folder as input before running the code

229 views (last 30 days)
ARP on 24 Apr 2018
Commented: ARP on 24 Apr 2018
Hi all,
I am not sure if the title is clear. Basically, every time I will run the code, I want a new folder to save my m.files and figures.
That new folder, should be called as the project name that I chose, for example "set_one". This should be easily changed at the beginning of the code, and not to be changed manually from all the related commands every time I change the data sets.
an example
fname = 'set_one'; % this should be the proyect title
Image data = [12.7 5.02 -98 63.9 0 -.2 56];
mkdir('C:\Users\Desktop\matlab results\',fname);
filename=('C:\Users\Desktop\matlab results\fname\Image_Data');
..And it does not work. mkdir does create a new folder as set_one, but is not able to recognize the new folder as set_one. Therefore, error message says that the folder does not exist.
What I am trying to do is that on the filename line, where it says fname, should be rewritten dynamically by the name I give to fname.
Thanks in advance,


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 24 Apr 2018
Edited: Stephen Cobeldick on 24 Apr 2018
The problem is that where you tried to access the new folder names you referred to the variable name, rather than to the value of that variable:
filename=('C:\Users\Desktop\matlab results\fname\Image_Data');
^^^^^^ this refers to a directory named 'fname'!
When you put fname into that char vector, then that is what MATLAB tells the OS to look for: a folder literally named 'fname' which should be inside the folder 'matlab results'.
Do you have such a directory?
Clearly not.
Is that the name of the directory that you are looking for?
Clearly not.
Instead of looking for a folder literally named 'fname', you need to use the value of the variable fname (not the name of the variable). I also recommend that you use fullfile, which makes joining folder names together easier:
pname = 'C:\Users\Desktop\matlab results';
dname = 'set_one';
Image data = [12.7 5.02 -98 63.9 0 -.2 56];
filename = fullfile(pname,dname,'Image_Data.mat');

  1 Comment

ARP on 24 Apr 2018
That is exactly what I needed. Many thanks!
I am still learning and I am not familiar with all the possible commands. Thanks for your time

Sign in to comment.

More Answers (1)

Peyman Obeidy
Peyman Obeidy on 24 Apr 2018
not quite sure what you want to do, but try this
path =[pwd '\'];
stru =dir( fullfile(path, '*.vsi') );
fileNames = { };
outputPath = [path 'NewImage2\'];
for iFile = 1: numel(fileNames)
fileName=fileNames{iFile}; % read the file name here
numofName=str2double(fileName(7:end-4)); % cut out all but the number
newName = fullfile([outputPath 'Image_' , sprintf('%02d.vsi', newFileNum)] );
copyfile(fullfile(path, fileNames{iFile}), newName );


ARP on 24 Apr 2018
To try to clarify on my problem. Every time I run the script, I want to create a new folder to save the final answers of my script in there.
The easiest would be to do manually, and change the folder name to what I need, but it is unpractical because there will be several commands along the script for saving relevant data.
Hence, I would like to name a folder at the beinning of the script, let say "set_one".
mkdir commands should pick up that folder name that I am giving, and create it for me.
Then I save my variables to desired folder.
Next day, I chose to call it "set_two". So all I have to do is change the folder name a the very beginning of my script.
I haven't tried your script, but I have several questions about it.
Peyman Obeidy
Peyman Obeidy on 24 Apr 2018

do you want your code to ask you the name? something like;

prompt = {'Enter a name of the folader'};
title = 'Folder name';
definput = {'Set_one'};
opts.Interpreter = 'tex';
answer = inputdlg(prompt,title,[1 40],definput,opts);
ARP on 24 Apr 2018

that would be useful yes, but the problem is that matlab does not recognize the new folder name. For instance,

fname = 'set_one' ; % the folder that I want to save the data, which does not exist. 
mkdir('C:\Users\Desktop\matlab results\',fname); % create that folder

Running as it is, a new folder "set_one" is created.


When saving a variable, now I tell it to go to the new folder given by fname, that is "set_one" in this case

filename=('C:\Users\Desktop\matlab results\fname\Image_Data');
Cannot create 'Image_Data.mat' because 'C:\Users\Desktop\matlab results\fname' does not exist.
Error (line 9)

To fix the error, I should write the command as (see set_one instead of fname)

filename=('C:\Users\Desktop\matlab results\set_one\Image_Data');

But that is not the way to go, since I will have to change manually every time the folder name, and its linked commands.

Why matlab does not understand that fname == set_one?

Sign in to comment.

Sign in to answer this question.


Translated by