mkdir and simulations problems

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 个评论

What have you tried so far? What is exactly the question?
This is my try :
clear
k = 1;
path = 'C:\Users\...\';
path = strcat(path, num2str(k), '\');
foldername = sprintf('prova%02',k);
foldername= strcat(path, num2str(k), '\');
prova="prova" + k;
mkdir ( prova );
k = k+1;
By doing this, is able to create the folders; the problem comes whe I try to add the figure and save it in the respective folder, like this:
figure
plot(rand(10,1))
picturename = fullfile(foldername, '1')
saveas (gcf, picturename, 'jpg')
Any help would be greatly appreciated.
Really guys? Not even a suggestion?
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?)
Stephen23
Stephen23 2019-9-10
编辑:Stephen23 2019-9-10
"the problem comes whe I try to add the figure and save it"
What exactly is the problem? What happens when you try your code?
Note that you should use fullfile rather than concatenating strings (it takes care of the path separators for you).
Guys, mine was just a request for help and I don't want it to be taken as a claim or something; if I did imply this, I apologize.
However, the error in question is the following:
Error using saveas
Invalid or missing path: C:\Users\nator\Desktop\aaa\foldername15\15\1
Error in Untitled
saveas (gcf, picturename, 'jpg')
Unfortunately I don't have a lot of expertise with Matlab, so I don't really know how to act
" followed by two lines of code that create a different folder in an arbitrary location"
Not using the two lines of code does not create the "numbered" folders

请先登录,再进行评论。

 采纳的回答

Stephen23
Stephen23 2019-9-11
编辑:Stephen23 2024-11-4
You could download my FEX submission nextname:
And simply use it something like this:
D = nextname('prova<1>');
mkdir(D)
plot(rand(10,1))
saveas(gcf,fullfile(D,'1.jpg'));
plot(rand(10,1))
saveas(gcf,fullfile(D,'2.jpg'));

更多回答(1 个)

Guillaume
Guillaume 2019-9-10
编辑:Guillaume 2019-9-10
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 个评论

By doing this i get:
Index in position 1 exceeds array bounds (must not exceed 1).
Error in Untitled
picturename = fullfile(foldername, '1.jpg');
"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.
The code is able to create the subfolders (prova1,prova2, ecc) but I can't save the plot every time in a different one; I need to, every time I run the code, create a new subfolder and then save the plot inside.
Please paste the code you're currently using so we can understand what the problem is now. Use the insert_code_16.png button to format code as code.
%clear
% k = 1
foldername = fullfile(pwd, sprintf('prova',k));
if exist(foldername, 'dir')
mkdir("prova"+k);
provab = "prova" +k;
cd(provab)
else
mkdir ( foldername );
end
figure('visible','off')
plot(rand(10,1))
saveas(gcf,'1','jpg');
%
figure('visible','off')
plot(rand(10,1))
saveas(gcf,'2','jpg');
%
save('k','k')
k=k+1
cd ../
I am currently using this code; in this case, however, it is not efficient. In fact, after the first simulation, it is necessary to comment k = 1
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 File Operations 的更多信息

产品

版本

R2018b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by