mkdir and simulations problems
Show older comments
Hi guys, I have a problem.
What I need is to create a code which is able, for every simulation, to:
- create a "numbered" folder (like Try_1, Try_2, ecc)
- inside the folder save two plots and a variable
- every time I press the run button should go to next folder and do it again
Also, it will be simple without for loops because I need to put all in a really heavy code.
Thank you!
PS : sorry for my bad english.
7 Comments
Alex Mcaulley
on 10 Sep 2019
What have you tried so far? What is exactly the question?
Giuseppe Pintori
on 10 Sep 2019
Edited: Guillaume
on 10 Sep 2019
Giuseppe Pintori
on 10 Sep 2019
Giuseppe Pintori
on 10 Sep 2019
Guillaume
on 10 Sep 2019
We're all volunteers here with various things to do. You should not have any expectation that your question will be answered within a timeframe. If you need that, hire a consultant.
Your question may also not get answered because it's badly formatted (first thing I had to do is go and edit it to make the code easier to read), is not clear enough or any other reason.
For example, here we have some convoluted code (you seem to know about fullfile, why isn't it used there?) to create a folder whose name is stored in foldername, followed by two lines of code that create a different folder in an arbitrary location (the current directory whatever that is). Then we have the sentence: the problem comes... with no description of what the problem is (an error? if so what is the full error message? something else? then what?)
Giuseppe Pintori
on 10 Sep 2019
Edited: Giuseppe Pintori
on 10 Sep 2019
Accepted Answer
More Answers (1)
As far as I can see the problem has nothing to do with matlab expertise but is a failure in your logic.
You create a folder name, which would be better achieved with:
k = ...
path = 'C:\Users\...\'; %note that path is also a matlab function. Another name would be better
foldername = fullfil(path, sprintf('prova%02', k));
Then you create another name with different formatting in the variable named prova. The above code will generate the name c:\Users\....\Prova01 for k = 1, whereas prova will be Prova1. Note the missing 0. You then forget about that foldername and mkdir that Prova1. Since prova is not a full path, it will be created in the current folder, whatever that is, most likely not path.
So, in addition to creating a folder with the different name you intended, you also create it in a different location. So, it's not surprise that when saving your figure, matlab tells you: sorry, that folder doesn't exist.
mkdir(foldername);
would probably solve the problem. There's never any need for that prova variable.
While we're at it, it would be clearer if you created picturename as:
picturename = fullfile(foldername, '1.jpg'); %explicitly add the extension
6 Comments
Giuseppe Pintori
on 10 Sep 2019
Stephen23
on 11 Sep 2019
"Index in position 1 exceeds array bounds (must not exceed 1)....picturename = fullfile(foldername, '1.jpg');"
It looks like you have named a variable fullfile. Delete that variable from the workspace.
Giuseppe Pintori
on 11 Sep 2019
Guillaume
on 11 Sep 2019
Please paste the code you're currently using so we can understand what the problem is now. Use the
button to format code as code.
button to format code as code.
Giuseppe Pintori
on 11 Sep 2019
Guillaume
on 11 Sep 2019
What are you trying to achieve with this:
if exist(foldername, 'dir')
mkdir("prova"+k);
provab = "prova" +k;
cd(provab)
else
mkdir ( foldername );
end
In particular, why have you got two different variables for the folder name, foldername and provab.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!