Concatenate variables by searching mkdir folder and adding to string, n+1
7 views (last 30 days)
Show older comments
Hello!
I am working on a script that has a few different calculations being done. Each one produces its own figure which is then saved into a folder using mkdir(). I would like to have the user only have to enter in the ID name and have the script auto-generate the names of the figures. Some ID folders already have figures already stores in them, so I want the code to do an 'n+1' process where it has the ID name and adds a number to the end. The number being the next available in order.
For example:
ID=Jerry
Circle_image_name = strcat('ID', 'Circle','n+1')
Energy_Image_Name = strcat('ID', 'Energy','n+1')
I am not sure how to create a loop that reads the files and determines what 'n' is equal to. I then need to use 'n' and insert it into a string and concatenate it to name the figures.
Here is the script:
filenamestr = inputdlg('Enter ID Number', 's'); %Prompts user to enter ID Number
ID_Name = filenamestr{:}; %Saves ID name as string
mkdir ('C:\Users\User\Desktop\Data\', ID_Name); %Uses string to create/use folder
CircFig=heatmap(Circle) %Creates circle figure
CircStr = inputdlg('Enter Circle Image Name', 's'); %Prompts user to enter circle name
Circle_Image_Name = CircStr{:}; %Stores circle name as string
saveas(CircFig, fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data', ID_Name, Circle_Image_Name), 'jpeg');
mapE = heatmap(Energy) %Creates energy figure
EnergyStr = inputdlg('Enter Energy Image Name', 's'); %Prompts user to enter energy name
Energy_Image_Name = EnergyStr{:}; %Stores energy name as string
saveas(mapE, fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data', ID_Name, Energy_Image_Name), 'jpeg');
0 Comments
Accepted Answer
Jan
on 24 Mar 2021
Edited: Jan
on 24 Mar 2021
Create a dedicated function for this job:
BaseFolder = fullfile( 'C:\', 'Users', 'User', 'Desktop', 'Data');
...
Circle_Image_Name = CircStr{:};
FileName = GetNumberedName(fullfile(BaseFolder, ID_Name), Circle_Image_Name, 'jpeg');
saveas(CircFig, FileName);
...
function FileName = GetNumberedName(Folder, Name, Ext)
FileList = dir(fullfile(Folder, [Name, '*.', Ext]);
Num = numel(FileList) + 1;
FileName = fullfile(Folder, sprintf('%s%d.%s', Name, Num, Ext));
while isfile(FileName) % Check, if this file is really not existing:
Num = Num + 1;
FileName = fullfile(Folder, sprintf('%s%d.%s', Name, Num, Ext));
end
end
It is saver to check the existence of a file at first. Maybe you have used the folder before already and a file has been deleted:
Folder\File1.jpeg % File2 was delete before
Folder\File3.jpeg
Then simply counting the number of files would produce a collision.
By the way, prefer leading zeros for indexed file names. Then the alphabetcial order is the numerical order also:
sprintf('%s%05d.%s', Name, Num, Ext)
4 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!