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

mkdirsave

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');
save(filename,'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,
Regards

Best Answer

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';
mkdir(fullfile(pname,dname))
Image data = [12.7 5.02 -98 63.9 0 -.2 56];
filename = fullfile(pname,dname,'Image_Data.mat');
save(filename,'Image_Data')